use*_*869 5 java android drawerlayout android-tablayout
每当我最初创建我的活动时,一切都很顺利,因为我能够正确地扩充我的布局.然而,问题是每当我切换到使用另一个应用程序,然后回到我自己的应用程序,我收到一个错误,说明TabLayout为空,导致我的应用程序崩溃.如果我切换到另一个应用程序几分钟,我DrawerLayout是空的,也导致我的应用程序崩溃.
如果我正确理解了Android生命周期,我意识到当活动从背景到前景时,活动会被重新创建,并且onCreate从这里引用该方法再次被调用:Android活动生命周期 - 这些方法的所有内容是什么?.但是,我不明白为什么当应用程序启动时,一切正常,但是当我进入后台时,或者将TabLayout其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);
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我确保findViewById它们都引用了正确的id,但我似乎无法弄清楚出了什么问题.奇怪的是:如果我快速切换app -> another app -> app,TabLayout则为null.如果我切换app -> another app -> wait a few minutes -> app,DrawerLayout则为null.我不知道为什么会这样!任何帮助,将不胜感激.
编辑:应用程序崩溃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)在 android.app.ActivityThread.access $ 900(ActivityThread.java:172)在Android.app.Handler.dispatchMessage(Handler.java:102)的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1421)上. os.Looper.loop(Looper.java:145)位于java.lang.reflect.Method的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:5835).在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)中调用(Method.java:372)
编辑:包含的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);
}
}
Run Code Online (Sandbox Code Playgroud)
您对抽屉和 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);
}
}
Run Code Online (Sandbox Code Playgroud)
假设工具栏是在类中的其他地方定义的,应该可以工作。
| 归档时间: |
|
| 查看次数: |
1237 次 |
| 最近记录: |