当前位置: 首页 > 知识库问答 >
问题:

从后台恢复活动后,DrawerLayout 或 TabLayout 是否为空?

司空叶五
2023-03-14

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

如果我对Android生命周期的理解正确的话,我意识到活动是在从后台到前台的时候被重新创建的,而< code>onCreate方法是从这个引用中再次调用的:Android activity Life Cycle——所有这些方法都是为了什么?。但是,我不明白为什么当应用启动时,一切都正常,但当我进入后台时,要么< code>TabLayout要么< code>DrawerLayout被设置为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);
    }
}

新闻_提要. 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>

我确保<code>findViewById</code>都引用了正确的id,但我似乎不知道出了什么问题。奇怪的是:如果我从<code>应用程序快速切换-

编辑:应用程序在我的活动的<code>onCreate<code>方法上崩溃。此外,我还提供了一个stacktrace:如下所示:

03-16 09:31:21.116 25852-25852/com . test . tabs . tabs I/system . out:news _ feed:toolbar:Android . support . V7 . widget . toolbar { 380 bcc 25V . E...........ID 0,0-0,0 # 7f 0d 009 c 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 . Drawer layout { 3ed 761 fa VFE...........一、0,0-0,0 # 7f 0d 0095 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 @ 22 EB 13 ab 03-16 09:31:21.116 25852-25852

编辑:包含的BatchAppCompatactive类:

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

共有1个答案

宰父跃
2023-03-14

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

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

 类似资料:
  • 情况: 比如说,我目前正在开展应用程序活动A。 过了一段时间,我按下了“主页”按钮。应用程序A进入后台 现在,我开始使用另一个应用程序B——例如youtube等。 在应用程序A中发生了一些事情(不管在这种情况下发生了什么,比方说计时器完成了计算时间),目前它被最小化到后台 事件发生时,应用程序A活动会自动从后台恢复 问题: 如何完成第5步?基本上,我需要知道如何以编程方式从后台恢复应用程序。 我试

  • 我有一个玩家活动和浮动玩家(服务),点击后退按钮后,玩家活动关闭,浮动玩家开始行动,当我点击浮动玩家时,我想回到玩家活动,但没有再次运行创建方法。 我在StackOver Flow中读到了很多解决方案,特别是使用FLAG\u ACTIVITY\u CLEAR\u TOP和FLAG\u ACTIVITY\u SINGLE\u TOP标志,或者将android:launchMode=“singleTa

  • 我通过以下代码阻止返回登录活动: 当用户处于第二个活动上并且他按下主页按钮,应用程序进入后台,当他恢复它时,第二个活动出现 - 没关系。但是,当用户在第二个活动中按后退按钮时,应用程序进入后台,当他重新喜欢它时 - 登录活动再次创建并且它处于前期。 如何防止再次创建登录活动?或者,我不应该完成登录活动,而是应该检查用户是否已登录-如果是,我应该显示第二个活动? 清单:

  • 我正在制作一个应用程序,使用ActivityRecognition API跟踪用户在后台的活动,如果用户在指定的时间段(例如1小时)内保持在同一位置,则系统会推送通知,告诉用户散步。我已经实现了活动识别,但仅适用于打开应用程序的情况。显然,Google API客户端需要保持连接才能发送活动更新。我的问题是-对于后台活动跟踪,什么是更好的解决方案: 1)在主活动(或单独活动)中实现警报管理器,该活动

  • 前提条件: AndroidQ 问题一: 我在前台有一个视图,当用户单击该视图时,我尝试在android Q中启动一个活动。 以下是我的代码。 它不工作,也不会发生碰撞。我有一些日志。 D ActivityTaskManagerServiceInjector: MIUILOG-权限拒绝活动:意图... 问题2: 我有一个TileService,当用户单击状态栏中的图标时,我尝试启动一个活动 雄激素单

  • 问题内容: 我在应用程序的状态栏中有一条通知: 这样做的问题是,当您从应用程序中按下主页按钮(将其推到后台)然后在从状态栏访问的列表中的通知上按下时,它将启动活动的新副本。我要做的就是恢复应用程序(例如,长按主屏幕按钮并按应用程序图标上的)。有没有一种创建意图的方法? 问题答案: 我已通过将我的活动更改为androidManifest.xml文件来解决了此问题。 此属性的默认值为,它允许运行任意数