Как же все таки равнодушным можно оставаться к программированию для Android? Ни одной функции просто взять и реализовать! Ни одной!
На этот раз есть абсолютно два одинаковых активити.xml .
activity_city.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".mainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.Base.AppBarOverlay"
app:elevation="0dp">
<include layout="@layout/toolbar"/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:id="@+id/searchLayout"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etSearch"
android:drawableLeft="@android:drawable/ic_menu_search"
android:hint="Название города"
android:background="@drawable/rounded_edittext"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15dp"
android:focusableInTouchMode="true"
android:elevation="0dp"
android:drawableTint="@color/secondary_text" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".mainActivity"
android:orientation="vertical"
android:padding="0dp">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:choiceMode="singleChoice"
android:layout_below="@+id/searchLayout"
android:divider="@color/divider"
android:dividerHeight="1dp"
android:focusableInTouchMode="true">
</ListView>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/stat_sys_phone_call" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_city2"
app:menu="@menu/activity_city2_drawer"/>
</android.support.v4.widget.DrawerLayout>
activity_sfera.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".mainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.Base.AppBarOverlay"
app:elevation="0dp">
<include layout="@layout/toolbar"/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:id="@+id/searchLayout"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etSearch"
android:drawableLeft="@android:drawable/ic_menu_search"
android:hint="Название города"
android:background="@drawable/rounded_edittext"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15dp"
android:focusableInTouchMode="true"
android:elevation="0dp"
android:drawableTint="@color/secondary_text" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".mainActivity"
android:orientation="vertical"
android:padding="0dp">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:choiceMode="singleChoice"
android:layout_below="@+id/searchLayout"
android:divider="@color/divider"
android:dividerHeight="1dp"
android:focusableInTouchMode="true">
</ListView>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/stat_sys_phone_call" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_city2"
app:menu="@menu/activity_city2_drawer"/>
</android.support.v4.widget.DrawerLayout>
В mainActivity.java :
protected void onResume() {
super.onResume();
if (mSettings.contains("city")) {
cityOption = mSettings.getString("city", null);
if (cityOption != null) {
if (hasConnection(getBaseContext())) {
setContentView(R.layout.activity_sfera);
toolbar = (Toolbar) findViewById(R.id.myToolBar);
toolbar.setTitleTextColor(getResources().getColor(R.color.toolbar_title));
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
etSearch = (EditText) findViewById(R.id.etSearch);
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
sferaAdapter.getFilter().filter(s);
}
});
get_sferaList();
} else {
setContentView(R.layout.layout_nointernet);
}
}
} else {
if (hasConnection(getBaseContext())) {
setContentView(R.layout.activity_city);
toolbar = (Toolbar) findViewById(R.id.myToolBar);
toolbar.setTitleTextColor(getResources().getColor(R.color.toolbar_title));
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
etSearch = (EditText) findViewById(R.id.etSearch);
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
cityAdapter.getFilter().filter(s);
}
});
get_cityList();
} else {
setContentView(R.layout.layout_nointernet);
}
}
}
То есть если в настройках есть название города, показываем activity_sfera, если в настройках названия города нет, то показываем activity_city. Почему если setContentView(R.layout.activity_city) работает, а если setContentView(R.layout.activity_sfera) завершает приложение с ошибкой:
Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference
setContentView()
в зависимости от настроек?Или я не правильно понял, и он у вас как установился(сетКонтент) так и статичен во время всей работы?