我正在尝试设置数组适配器并具有以下代码:
private String[] mServices;
private ListView mDrawerList;
//Ends Here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//For Navigation Drawer
mServices = getResources().getStringArray(R.array.drawer_services);
mDrawerList = (ListView) findViewById (R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mServices));
我尝试设置Adapter的最后一行代码,我得到一个NullPointerException。如果我尝试导航到包含此代码的页面,应用程序将崩溃。
任何指针?
这是调试:
07-08 20:05:22.885: E/AndroidRuntime(2351): FATAL EXCEPTION: main
07-08 20:05:22.885: E/AndroidRuntime(2351): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parrot.urlripper/com.parrot.urlripper.google}: java.lang.NullPointerException
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.os.Looper.loop(Looper.java:123)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-08 20:05:22.885: E/AndroidRuntime(2351): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 20:05:22.885: E/AndroidRuntime(2351): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 20:05:22.885: E/AndroidRuntime(2351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-08 20:05:22.885: E/AndroidRuntime(2351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-08 20:05:22.885: E/AndroidRuntime(2351): at dalvik.system.NativeStart.main(Native Method)
07-08 20:05:22.885: E/AndroidRuntime(2351): Caused by: java.lang.NullPointerException
07-08 20:05:22.885: E/AndroidRuntime(2351): at com.parrot.urlripper.google.onCreate(google.java:35)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-08 20:05:22.885: E/AndroidRuntime(2351): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
答案 0 :(得分:0)
在ArrayAdapter的构造函数中,您必须按照Android文档中的描述传递TextView标识: ArrayAdapter
互联网上有一些关于这个主题的教程,例如这个:ArrayAdapter basic tutorial
您的问题也可能来自mServices
,可以为空。
编辑:
以下代码适用于我:
活动:
public class MainActivity extends Activity {
private String[] mServices = new String[] {"Test1", "Test2", "Test3", "Test4"};
private ListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.text_view, mServices));
}
}
activity_main.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:choiceMode="multipleChoice"
/>
</LinearLayout>
和text_view.xml文件:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:textColor="@android:color/black"
android:textStyle="italic">
</TextView>
答案 1 :(得分:0)
mDrawerList.setAdapter(new ArrayAdapter(this,R.layout.drawer_list_item,{“test”,“test”,“test”}));
如果这解决了你的问题,那么mServices数组就是问题..