Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从后台恢复活动后,抽屉布局或表格布局是否为空?_Java_Android_Drawerlayout_Android Tablayout - Fatal编程技术网

Java 从后台恢复活动后,抽屉布局或表格布局是否为空?

Java 从后台恢复活动后,抽屉布局或表格布局是否为空?,java,android,drawerlayout,android-tablayout,Java,Android,Drawerlayout,Android Tablayout,每当我开始创建我的活动时,一切都很顺利,因为我能够正确地膨胀我的布局。然而,问题是每当我切换到使用另一个应用程序,然后返回到我自己的应用程序时,我会收到一个错误,说TabLayout为空,导致我的应用程序崩溃。如果我切换到另一个应用程序几分钟,我的DrawerLayout为空,也会导致我的应用程序崩溃 如果我正确理解了Android的生命周期,我意识到当活动从后台转到前台时会重新创建,并且会再次调用onCreate方法,如下所述:。然而,我不明白为什么当应用程序启动时,一切正常,但当我进入后台时

每当我开始创建我的活动时,一切都很顺利,因为我能够正确地膨胀我的布局。然而,问题是每当我切换到使用另一个应用程序,然后返回到我自己的应用程序时,我会收到一个错误,说
TabLayout
为空,导致我的应用程序崩溃。如果我切换到另一个应用程序几分钟,我的
DrawerLayout
为空,也会导致我的应用程序崩溃

如果我正确理解了Android的生命周期,我意识到当活动从后台转到前台时会重新创建,并且会再次调用
onCreate
方法,如下所述:。然而,我不明白为什么当应用程序启动时,一切正常,但当我进入后台时,
TabLayout
DrawerLayout
设置为空。以下是我的尝试:

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>
<?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>

您对抽屉和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);
    }
}

假设工具栏是在类中的其他地方定义的,则应该可以工作。

不知道它是否与您的错误有关,但调用
toggle.syncState()应该在
活动的
onpostreate()
方法中完成,正如文档所说,根据您的代码,您没有保存对
抽屉布局
表格布局
的任何引用。以后如何访问
onCreate
方法之外的方法?。同样,并非每次活动从后台到前台时都会调用Activity recreate和
onCreate
。只有当活动被破坏时才会发生。请将代码张贴在您访问
TabLayout
DrawerLayout
的位置,该代码在哪里为空,应用程序在哪里崩溃?在一次创建中?如果您切换App->other App->App onCreate,则不应再次调用,除非您在开发人员设置中启用了“请勿保留活动”。还请提供崩溃日志和它所在的代码行crashes@DavidMedenjak是的,它在onCreate中崩溃了,我不知道为什么会再次调用它。我的代码中也没有启用“不保留活动”选项。@user1871869您能提供stacktrace吗?这个代码看起来非常好。您是否也在其他地方调用
findviewbyd
并对这些视图进行处理?为什么我的声明很奇怪?您对如何声明它有什么建议吗?@user1871869 drawer和tablayout在类外声明(我假设这是一个拼写错误,除非news_feed是一个内部类),它们被声明为私有静态。在Oncreate方法中,您还重新声明了tablayout。@user1871869上述解决方案解决了您的问题吗?我也面临着和你们类似的问题。
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);
    }
}