Android 工具栏和导航抽屉
我试图在使用NavigationFragment的现有项目中包含材料设计。 所以我尝试使用工具栏而不是ActionBar。 我遵循指南,将所有Android 工具栏和导航抽屉,android,android-fragments,fragment,navigation-drawer,material-design,Android,Android Fragments,Fragment,Navigation Drawer,Material Design,我试图在使用NavigationFragment的现有项目中包含材料设计。 所以我尝试使用工具栏而不是ActionBar。 我遵循指南,将所有getActionBar()替换为getSupportActionBar(),但我的应用程序总是在启动时崩溃 activity_main.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
getActionBar()
替换为getSupportActionBar()
,但我的应用程序总是在启动时崩溃
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.andreapivetta.mypckg.MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/navigation_drawer"
android:name="com.andreapivetta.mypckg.NavigationDrawerFragment"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
</android.support.v4.widget.DrawerLayout>
日志
我做错了什么?在您的
onCreate
中,在尝试访问工具栏视图之前,您从未设置过内容
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mSharedPreferences = getSharedPreferences("MyPref", 0);
setContentView(R.layout.activity_main);
您需要先为活动设置内容,然后才能访问它
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mSharedPreferences = getSharedPreferences("MyPref", 0);
您必须按照tyczy的建议更改onCreate()方法中的代码 您的布局还有另一个问题。 您在抽屉布局中使用了3个视图,而应该使用2个视图 您必须更改布局,例如:
<android.support.v4.widget.DrawerLayout>
<LinearLayout>
<Toolbar..>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="com.andreapivetta.mypckg.NavigationDrawerFragment"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</DrawerLayout>
我也有类似的问题。解决这个问题的方法是,我将
NavigationDrawerFragment
中的selectItem(…)
方法从onCreateView()
移动到onActivityCreated()
你需要显示你的代码,特别是
MainActivity.java:155
@tyczj我刚刚添加了MainActivity最重要的部分你在你的MainActivity中导入了哪个工具栏,android.support.v7.widget.Toolbar?谢谢,我更改了它并删除了ActionBar ActionBar=getActionBar();actionBar.setDisplayHomeAsUpEnabled(true);actionBar.setHomeButtonEnabled(真);从我的NavigationDrawer,现在应用程序没有崩溃,但工具栏有屏幕的高度:驻留你不应该将工具栏放在抽屉布局中,不管怎样,它应该在外面。你是对的。我现在无法测试我的代码,我会尽快将您的答案标记为正确。我在使用时遇到了相同的问题。必须定义一个方法并用@ViewById为注入的视图注释它,因为这些字段没有在onCreate()方法中初始化是的答案是正确的,但我已经这样做了,这要感谢tyczj注释:)
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
mSharedPreferences = getSharedPreferences("MyPref", 0);
<android.support.v4.widget.DrawerLayout>
<LinearLayout>
<Toolbar..>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="com.andreapivetta.mypckg.NavigationDrawerFragment"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</DrawerLayout>
@Override
public void onActivityCreated(Bundle savedInstanceState) {
selectItem(mCurrentSelectedPosition);
super.onActivityCreated(savedInstanceState);
}