whenever I create my activity initially, everything goes well, because I can inflate my layouts correctly. However, the problem is that when I switch to using another application and then return to my own application, I get an error message indicating that TabLayout
is null, which causes my application to crash. If I switch to another application in a few minutes, my DrawerLayout
is null, which also causes my application to crash.
If I correctly understood the Android life cycle, I understand that activity is recreated when it goes from the background to the foreground, and the onCreate
method onCreate
again called a link: Android Activity Life Cycle - what are all these methods for? . However, I donโt understand why when the application starts, everything works fine, but when I go into the background, either the TabLayout
or DrawerLayout
set to null. Here is my attempt:
news_feed.java:
private static DrawerLayout drawer; private static TabLayout tabLayout; public class news_feed extends BatchAppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_feed); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
news_feed.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" tools:openDrawer="start"> <include layout="@layout/app_bar_news_feed" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/app_bar_main" /> <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"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/nav_header_friends_list" android:id="@+id/navigation_header"/> <ListView android:id="@+id/friends_list" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>
app_bar_news_feed.xml:
<?xml version="1.0" encoding="utf-8"?> <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=".news_feed"> <RelativeLayout android:id="@+id/main_layout" 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.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay" android:id="@+id/app_bar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/app_bar" android:background="?attr/colorPrimary" android:elevation="6dp" android:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_below="@id/tab_layout"/> </RelativeLayout> <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/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
I made sure that findViewById
all refer to the correct identifier, but I cannot figure out what is going wrong. Strange: if I quickly switch from app -> another app -> app
, then TabLayout
is null. If I switch from app -> another app -> wait a few minutes -> app
, then DrawerLayout
is null. I have no idea why this is happening! Any help would be appreciated.
Edit: The application breaks into the onCreate
method of my activity. In addition, I provided stacktrace: as follows here:
03-16 09: 31: 21.116 25852-25852 / com.test.tabs.tabs I / System.out: news_feed: toolbar: android.support.v7.widget.Toolbar {380bcc25 VE ........ .. ID 0,0-0,0 # 7f0d009c app: id / toolbar} 03-16 09: 31: 21.116 25852-25852 / com.test.tabs.tabs I / System.out: news_feed: Box: android. support.v4.widget.DrawerLayout {3ed761fa VFE ........... Me. 0.0-0.0 # 7f0d0095 app: id / drawer_layout} 03-16 09: 31: 21.116 25852-25852 / com.test.tabs.tabs I / System.out: news_feed: toggle: com.test.tabs.tabs.com.tabs.activity.news_feed$1@22eb13ab 03- 16 09: 31: 21.116 25852-25852 / com.test.tabs.tabs I / System.out: news_feed: Layout tab: null 03-16 09: 31: 21.116 25852-25852 / com.test.tabs.tabs E / AndroidRuntime: FATAL EXCEPTION: main Process: com.test.tabs.tabs, PID: 25852 java.lang.RuntimeException: Unable to start Activity ComponentInfo {com.test.tabs.tabs / com.test.tabs.tabs.com.tabs. activity.news_feed}: java.lang.NullPointerException: attempt to call the virtual method 'android.support.design.widget.TabLayout $ Tab android.support.design.widget.TabLayout.newTab ()' to reference a null object at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2661) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2726) at android.app.ActivityThread.access $ 900 (ActivityThread.java:172) at android.app .ActivityThread $ H.handleMessage (ActivityThread.java:1421) on android.os.Handler.dispatchMessage (Handler.java:102) on android.os.Looper.loop (Looper.java:145) at android.app.ActivityThread. main (ActivityThread.java:5835) in java.lang.reflect.Method.invoke (native method) in java.lang.reflect.Method.invoke (Method.javaI7272) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 1399) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1194) Called: java.lang.NullPointerException: attempt to call the virtual method 'android.support.design.widget.TabLayout $ Tab android.support.design .widget.TabLayout.newTab () 'for a reference to a null object at com.test.tabs.tabs.com.tabs.activity.news_feed.onCreate (news_feed.java: 174) at android.app.Activity.performCreate (Activity.java:6221) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2614) at android. app.ActivityThread.handleLaunchActivity (ActivityThread.java:2726) at android.app.ActivityThread.access $ 900 (ActivityThread.java:172) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1421) on android.os. Handler.dispat chMessage (Handler.java:102) on android.os.Looper.loop (Looper.java:145) on android.app.ActivityThread.main (ActivityThread.java:5835) in java.lang.reflect.Method.invoke (native method) in java.lang.reflect.Method.invoke (Method.javahaps72) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit .main (ZygoteInit.java:1194)
Edit: The BatchAppCompatActivity class is included:
public class BatchAppCompatActivity extends AppCompatActivity { @Override protected void onStart() { super.onStart(); Batch.onStart(this); } @Override protected void onStop() { Batch.onStop(this); super.onStop(); } @Override protected void onDestroy() { Batch.onDestroy(this); super.onDestroy(); } @Override protected void onNewIntent(Intent intent) { Batch.onNewIntent(this, intent); super.onNewIntent(intent); } }