我正在尝试使用android支持库创建一个抽屉导航到姜饼项目我已经成功安装了支持库并且我试图将一个字符串数组传递到抽屉列表但是我得到一个空指针异常数组,不知道为什么。有没有人知道如何在旧版Android中正确实现抽屉布局?
到目前为止我所做的事情并不确定它是否是正确的方法。mainpage.java
String[] mPlanetTitles = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
//mPlanetTitles = getResources().getStringArray(R.array.menu_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, android.R.id.text1, mPlanetTitles));
}
drawer_list_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
错误日志
10-06 11:19:45.869: E/AndroidRuntime(1660): FATAL EXCEPTION: main
10-06 11:19:45.869: E/AndroidRuntime(1660): java.lang.NullPointerException
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.AbsListView.obtainView(AbsListView.java:1536)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.ListView.makeAndAddView(ListView.java:1793)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.ListView.fillDown(ListView.java:718)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.ListView.fillFromTop(ListView.java:775)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.ListView.layoutChildren(ListView.java:1646)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.AbsListView.onLayout(AbsListView.java:1366)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.view.View.layout(View.java:7175)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:678)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.view.View.layout(View.java:7175)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.view.View.layout(View.java:7175)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.view.View.layout(View.java:7175)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.os.Looper.loop(Looper.java:123)
10-06 11:19:45.869: E/AndroidRuntime(1660): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-06 11:19:45.869: E/AndroidRuntime(1660): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 11:19:45.869: E/AndroidRuntime(1660): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 11:19:45.869: E/AndroidRuntime(1660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-06 11:19:45.869: E/AndroidRuntime(1660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-06 11:19:45.869: E/AndroidRuntime(1660): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
在您的.java文件NavigationDrawer适配器上引用 android.R.id.text1 但在.xml文件中你有“@ + id / textView1”所以它抛出一个空指针异常。
将.xml文件TextView id更改为 android:id =“@ android:id / text1”,它应该开始工作。