0

Im am trying to create a drawer navigation into a gingerbread project using the android support library i have succcesfully installed the support library and am trying to pass a string array into the drawer list however i'm getting a null pointer exception in the array and not sure why. Does anyone know how to implement a drawer layout correctly within older versions of Android?

heres what i have done so far not sure if its the right way to do this.

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>

Error LOG

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)
4

1 回答 1

2

在您的 .java 文件 NavigationDrawer 适配器上,您正在引用android.R.id.text1但在您的 .xml 文件上,您有“@+id/textView1”,因此它会引发空指针异常。

将 .xml 文件 TextView id 更改为android:id="@android:id/text1"它应该开始工作。

于 2013-10-06T10:37:48.973 回答