whenever I create my activity initially, everything goes fine as I am able to inflate my layouts correctly. However, the issue is whenever I switch to using another app, then go back to my own app, I get an error saying that the TabLayout
is null, causing my app to crash. If I switch to another app for a few minutes, my DrawerLayout
is null, also causing my app to crash.
If I understand the Android Life Cycle correctly, I realize that the activity is recreated when it goes from background to foreground, and the onCreate
method is called again as referenced from this: Android activity life cycle - what are all these methods for?. However, I don't understand why when the app starts, everything works fine, but when I go into the background, either the TabLayout
or the DrawerLayout
is 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);
//Listen for navigation events
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
};
System.out.println("news_feed: toolbar: " + toolbar);
System.out.println("news_feed: Drawer: " + drawer);
System.out.println("news_feed: toggle: " + toggle);
drawer.setDrawerListener(toggle);
toggle.syncState();
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
System.out.println("news_feed: Tab Layout: " + tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("First"));
tabLayout.addTab(tabLayout.newTab().setText("Second"));
tabLayout.addTab(tabLayout.newTab().setText("Third"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
}
}
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>
<!-- <include layout="@layout/content_news_feed" /> -->
<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 the findViewById
's are all referencing the correct id's, but I can't seem to figure what is going wrong. It's strange: if I quickly switch from app -> another app -> app
, then the 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 occuring! Any help would be appreciated.
Edit: The app crashes on the onCreate
method of my activity. Moreover, I have provided a stacktrace: as followed 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 V.E..... ......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: Drawer: android.support.v4.widget.DrawerLayout{3ed761fa VFE..... ......I. 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: Tab Layout: 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 invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference 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) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5835) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference 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) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5835) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Edit: Included BatchAppCompatActivity class:
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);
}
}
Your declaration of the drawer and tabLayout variables seems a bit strange. Unless it's a typo I'm surprised it even works. Something like
public class news_feed extends BatchAppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private TabLayout tabLayout;
@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);
//Listen for navigation events
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
};
System.out.println("news_feed: toolbar: " + toolbar);
System.out.println("news_feed: Drawer: " + drawer);
System.out.println("news_feed: toggle: " + toggle);
drawer.setDrawerListener(toggle);
toggle.syncState();
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
System.out.println("news_feed: Tab Layout: " + tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("First"));
tabLayout.addTab(tabLayout.newTab().setText("Second"));
tabLayout.addTab(tabLayout.newTab().setText("Third"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
}
}
should work assuming toolbar is defined somewhere else in your class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With