簡體   English   中英

從后台恢復活動后,DrawerLayout或TabLayout為null?

[英]DrawerLayout or TabLayout is null after resuming activity from background?

每當我最初創建我的活動時,一切都很順利,因為我能夠正確地擴充我的布局。 然而,問題是每當我切換到使用另一個應用程序,然后回到我自己的應用程序,我得到一個錯誤說TabLayout為空,導致我的應用程序崩潰。 如果我切換到另一個應用程序幾分鍾,我的DrawerLayout為空,也導致我的應用程序崩潰。

如果我正確理解Android生命周期,我意識到當活動從背景到前景時重新創建活動,並且再次調用onCreate方法,如下所示: Android活動生命周期 - 所有這些方法是什么? 但是,我不明白為什么當應用程序啟動時,一切正常,但是當我進入后台時, TabLayoutDrawerLayout都設置為null。 這是我的嘗試:

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>

我確保findViewById都引用了正確的id,但我似乎無法弄清楚出了什么問題。 這很奇怪:如果我快速從app -> another app -> app切換,那么TabLayout為空。 如果我從app -> another app -> wait a few minutes -> app切換app -> another app -> wait a few minutes -> app ,那么DrawerLayout為空。 我不知道為什么會這樣! 任何幫助,將不勝感激。

編輯:應用程序在我的活動的onCreate方法崩潰。 而且,我提供了一個stacktrace:如下所示:

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:Drawer:android.support。 v4.widget.DrawerLayout {3ed761fa VFE .......我。 0,0-0,0#7f0d0095應用程序: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進程:com.test.tabs.tabs,PID:25852 java.lang.RuntimeException:無法啟動活動ComponentInfo {com.test.tabs .tabs / com.test.tabs.tabs.com.tabs.activity.news_feed}:java.lang.NullPointerException:嘗試調用虛方法'android.support.design.widget.TabLayout $ Tab android.support.design.widget .TabLayout.newTab()'在android.app.A活動中的android.app.ActivityThread.perleLaunchActivity(ActivityThread.java:2661)上的空對象引用,位於android.app.ActivityThread.access的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) $ 900(ActivityThread.java:172)在android.app.ActivityThread $ H.handleMessage(活動 Thread.java:1421)android.app.Handler.dispatchMessage(Handler.java:102)android.app.Looper.loop(Looper.java:145)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)引起:java.lang.NullPointerException:嘗試調用虛擬方法'android.support.design.widget.TabLayout $ Tab android。 support.design.widget.TabLayout.newTab()'在android.app.Activity.performCreate的com.test.tabs.tabs.com.tabs.activity.news_feed.onCreate(news_feed.java:174)上的空對象引用上(Activity.java:6221)在Android.app.ActivityThread.perleLaunchActivity(ActivityThread.java:2614)的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2726)a t android.app.ActivityThread.access $ 900(ActivityThread.java:172)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1421)在android.os.Handler.dispatchMessage(Handler.java:102)在android位於android.app.ActivityThread.main(ActivityThread.java:5835)的.os.Looper.loop(Looper.java:145)位於java.lang.reflect.Method的java.lang.reflect.Method.invoke(Native 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)

編輯:包含的BatchAppCompatActivity類:

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

您對抽屜和tabLayout變量的聲明似乎有點奇怪。 除非它是一個錯字,我甚至感到驚訝它甚至有效。 就像是

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

應該工作假設工具欄是在你班級的其他地方定義的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM