2

当我发起ListAdapter<String>这样的事情时,一切都很好:

初始化变量

List<String> deviceList = new LinkedList<String>();
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
            R.layout.deviceconnect,
            android.R.layout.simple_list_item_1, deviceList);

但是当我像这样在列表中添加一些东西时:

向列表中添加了一个项目

List<String> deviceList = new LinkedList<String>();
deviceList.add("Test");
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
            R.layout.deviceconnect,
            android.R.layout.simple_list_item_1, deviceList);

它抛出一个空指针异常,如下所示:

例外

09-11 22:02:40.267: E/AndroidRuntime(3052): java.lang.NullPointerException
09-11 22:02:40.267: E/AndroidRuntime(3052):     at          android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
09-11 22:02:40.267: E/AndroidRuntime(3052):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-11 22:02:40.267: E/AndroidRuntime(3052):     at android.widget.AbsListView.obtainView(AbsListView.java:2267)
09-11 22:02:40.267: E/AndroidRuntime(3052):     at android.widget.ListView.makeAndAddView(ListView.java:1769)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.ListView.fillDown(ListView.java:672)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.ListView.fillFromTop(ListView.java:733)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.ListView.layoutChildren(ListView.java:1622)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.AbsListView.onLayout(AbsListView.java:2102)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.View.layout(View.java:13754)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.View.layout(View.java:13754)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.View.layout(View.java:13754)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.View.layout(View.java:13754)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.View.layout(View.java:13754)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewGroup.layout(ViewGroup.java:4362)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.Choreographer.doFrame(Choreographer.java:525)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.os.Handler.handleCallback(Handler.java:615)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.os.Looper.loop(Looper.java:137)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at java.lang.reflect.Method.invoke(Method.java:511)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 22:16:54.689: E/AndroidRuntime(6064):     at   dalvik.system.NativeStart.main(Native  Method)

有人可以帮我解决这个问题吗?谢谢。

4

1 回答 1

2

你应该有这样的东西:

布局文件:

设备连接.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
                android:id="@+id/textView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
</LinearLayout>

这在你的Java中:

List<String> deviceList = new LinkedList<String>();
deviceList.add("Test");
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
            R.layout.deviceconnect,
            R.id.textView, 
            deviceList);
于 2012-09-11T17:25:49.427 回答