How can you still remain indifferent to programming for Android? Not a single function just to take and implement! None!

This time there are absolutely two identical activites.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> 

In 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); } } } 

That is, if there is a city name in the settings, we show activity_sfera, if there is no city name in the settings, then we show activity_city. Why if setContentView (R.layout.activity_city) works, and if setContentView (R.layout.activity_sfera) terminates the application with an error:

Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener (android.support.v4.widget.DrawerLayout $ DrawerListener)' on a null object reference

  • Do you want to say setContentView() you change the setContentView() programmatically during the program setContentView() ? Or I misunderstood, and how have you established it (setContent) and is it static during the whole work? - Silento
  • Depending on the settings, set setContentView () - Newbie
  • By the way, the same error is observed if you start activity_sfera as a new activit, via startActivity () - Newbie

2 answers 2

You have an incorrectly designed application architecture.

To begin with, the actions that you perform in onResume() need to be transferred to onCreate() , because after stopping and resuming the application, the user will see new data each time (re-creation of objects).

Secondly, in the development of setContentView() used once for each activation.

setContentView() must be set before the action in the activation, otherwise you run the risk of running into NPE.

 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); 

As mentioned above - setContentView() for each activation should be one. Without manipulations like this:

  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); } } } 

Want to indicate that the user has no internet? You can use a lot of other ways, starting with displaying a simple dialogue, or using fragments.

About the error. For me, this code is hard to read, but I am sure that the problem is in the initially incorrect approach.

  • I would also hard to read this code if I didn’t write it myself, I assure you, when launching the application, setContentView () is called once (layout_nointernet contains a button but when you click on it, finish () and then startActivity () are called, and depending on the settings only one setContentView is started). As for the architecture, in response to your first comment, I wrote that the same error crashes even if we run activity_sfera as a separate activation. Now I will try to transfer to onCreate, but I doubt that it will help. - Newbie
  • As it was supposed, the transfer to the onCreate method did not give anything. - Newbie
  • @ Newbie gave a more correct architectural approach, in the future you will say (perhaps) thanks. Try to simulate when 1 setContentView (); It works, then make a button that translates to another activation, with the same markup. In short, divide into two to work, then gradually transfer. - Silento pm
  • It will seem strange to you, like me, but initially I had it all separately. On the second activation, this error took off, which made me combine it all into an activation, where everything worked. if this application is started as it was designed by me then: 1. If there are no settings we need, then by condition the activity_city starts, and everything will work (just as it was with separate activations; 2. If the setting you are looking for is, then conditionally, setContentView (R.layout.activity_sfera) should be fulfilled and crashes with the above error, as is the case with separate activations - Beginner
  • The content of both activations IDENTICALLY what you can be sure of - Novice

In general, I do not know who else might be confronted with such nonsense, but the problem was resolved when I switched places of activations in the manifesto.