我正在尝试将旧应用程序移至新的Material设计指南。我之前使用导航抽屉模板来创建主要活动。但是,我在尝试将工具栏实现到主Activity时遇到了一些大问题。我不断获得NullPointer Exceptions。
我正在创建一个这样的工具栏xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue" >
</android.support.v7.widget.Toolbar>
之后我尝试使用include命令将其添加到我的主布局:
<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.medinfo.main.MainActivity" >
<include layout="@layout/toolbar_database_edit" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/navigation_drawer"
android:name="com.medinfo.fragments.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
在我的activity_main中,我正在执行以下操作: 当我调用setSupportActionBar或调用Setup NavDrawer时,NPE正在发生。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
setSupportActionBar(toolbar);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}
我尝试在一个线性布局中包装DrawerLayout,这样做是在包含我的片段的容器后面创建工具栏,当我开始活动时,我看到在后台渲染的工具栏然后隐藏了容器和片段后加载。然而它确实删除了NPE。我还尝试在布局中创建工具栏,而不是使用单独的工具栏XML文件。我仍然遇到相同的NPE错误。
当我询问新的Material Design Stuff时,我还想知道是否有办法让我的EditText Widgets和Spinner Widgets使用旧的Holo Theme / Style。
我真的希望有一种简单的方法可以恢复这些风格。
感谢大家的帮助
答案 0 :(得分:4)
1)首先,您的布局不正确。 DrawerLayout接受两个子节点:布局容器和抽屉布局本身。
简化,
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/activity" />
<include layout="@layout/drawer" />
</android.support.v4.widget.DrawerLayout>
其次,您错误地findViewById
工具栏。
Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
您在这里引用布局,但应引用id。
鉴于上面的布局资源,我们有activity.xml
:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/blue"
android:foreground="?android:attr/windowContentOverlay"
app:theme="@style/Toolbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
. . . contents of activity layout
</FrameLayout>
</LinearLayout>
在代码中:
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
2)要使用旧的Holo样式,您应该使用Holo主题作为styles.xml中app主题的父级。但是用户会希望每个Android版本都有原生主题,所以这是一个不好的做法。