我使用导航抽屉作为我的应用程序的主要布局(内置新的ADT March更新)。我正在尝试按如下方式设置arrayadapter:
String[] items = new String[]{getString(R.string.home), getString(R.string.map), getString(R.string.update), getString(R.string.settings)};
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
R.layout.list_view,
R.id.list_item,
items
));
/res/layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:textColor="#000000"
android:padding="5dp"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"
android:textSize="20sp"/>
/res/layout/list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#C0C0C0"
android:dividerHeight="1dp"
android:background="#000000" />
我的目标是尝试默认替换应用基础附带的android.R.layout.simple_list_item_activated_1
android.R.id.text1
。
修改
我得到的错误是:
03-11 18:27:32.516: E/AndroidRuntime(28533): FATAL EXCEPTION: main
03-11 18:27:32.516: E/AndroidRuntime(28533): Process: com.unofficiallycoded.howsthewait, PID: 28533
03-11 18:27:32.516: E/AndroidRuntime(28533): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.ListView.makeAndAddView(ListView.java:1790)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.ListView.fillDown(ListView.java:691)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.ListView.fillFromTop(ListView.java:752)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.ListView.layoutChildren(ListView.java:1630)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.View.layout(View.java:14817)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewGroup.layout(ViewGroup.java:4631)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:714)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.View.layout(View.java:14817)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewGroup.layout(ViewGroup.java:4631)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.View.layout(View.java:14817)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewGroup.layout(ViewGroup.java:4631)
03-11 18:27:32.516: E/AndroidRuntime(28533): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.View.layout(View.java:14817)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewGroup.layout(ViewGroup.java:4631)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.View.layout(View.java:14817)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewGroup.layout(ViewGroup.java:4631)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.Choreographer.doFrame(Choreographer.java:544)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.os.Handler.handleCallback(Handler.java:733)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.os.Handler.dispatchMessage(Handler.java:95)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.os.Looper.loop(Looper.java:136)
03-11 18:27:32.516: E/AndroidRuntime(28533): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-11 18:27:32.516: E/AndroidRuntime(28533): at java.lang.reflect.Method.invoke(Native Method)
03-11 18:27:32.516: E/AndroidRuntime(28533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-11 18:27:32.516: E/AndroidRuntime(28533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
答案 0 :(得分:1)
第一个资源ID应该是我猜的列表项。
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
R.id.list_item,
items ));