恢复申请时遇到问题。我可以很好地创建我的活动,但出于某种原因,当我将我的应用程序放在后台时,等待几分钟,然后让它回到前置DrawerLayout
,始终为空。我第一次加载我的应用程序似乎工作正常,但我似乎无法弄清楚当我从后台重新创建活动时出了什么问题。这是我的代码:
MainActivity.Java:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Listen for navigation events
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
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);
...
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
...
}
System.out.println("Toolbar: " + toolbar);
System.out.println("Drawer: " + drawer);
System.out.println("Toggle: " + toggle);
drawer.setDrawerListener(toggle);
toggle.syncState();
}
activity_main.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" />
<include layout="@layout/include_progress_overlay"/>
<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>
第一次加载应用程序时,我可以在打印出"Drawer: "
时成功打印出一个对象,但是当我将它设置为背景然后让它在几分钟后回到前台,似乎打印出null,我不知道为什么。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:0)
您必须像这样重新充气XML布局。
试试这个,这是2行,替换你的setContentView
首先获取View Group
ViewGroup container = (ViewGroup)view.getParent();
现在我们需要扩充您的XML
setContentView(View.inflate(getApplicationContext(),R.layout.yourlayout,container));