0

这是我的代码:
这是我的 handleMessage 函数:

 public void handleMessage(Message msg)
                   {
                       if(msg.what==1){
                      ArrayList<pack.Form> o=(ArrayList<pack.Form>)msg.obj;
                      pb.setVisibility(8);try{

                      showList(o);
                      }catch(Exception e)
                      {
                          Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                      }
}

这是我的 showList 函数:

public  void showList(ArrayList<Form> v)
       {
           try{ListView listView = (ListView) ListActivity.this.findViewById(R.id.mylist);
            listView.setTextFilterEnabled(true);
             String[] values=new String[200];
             int i=0;
            Iterator<Form> it=v.listIterator();
            while(it.hasNext())
            {
                values[i]=((Form)it.next()).body;
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                    R.layout.list_item,values);

                // Assign adapter to ListView
                listView.setAdapter(adapter);}
           catch(Exception e){Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();}
       }

调用 showList() 函数时抛出空指针异常。问题是什么?
日志输出

D/AndroidRuntime(  914): Shutting down VM
W/dalvikvm(  914): threadid=3: thread exiting with uncaught exception (group=0x4
001b188)
E/AndroidRuntime(  914): Uncaught handler: thread main exiting due to uncaught e
xception
E/AndroidRuntime(  914): java.lang.NullPointerException
E/AndroidRuntime(  914):        at android.widget.ArrayAdapter.createViewFromRes
ource(ArrayAdapter.java:355)
E/AndroidRuntime(  914):        at android.widget.ArrayAdapter.getView(ArrayAdap
ter.java:323)
E/AndroidRuntime(  914):        at android.widget.AbsListView.obtainView(AbsList
View.java:1256)
E/AndroidRuntime(  914):        at android.widget.ListView.measureHeightOfChildr
en(ListView.java:1147)
E/AndroidRuntime(  914):        at android.widget.ListView.onMeasure(ListView.ja
va:1060)
E/AndroidRuntime(  914):        at android.view.View.measure(View.java:7964)
E/AndroidRuntime(  914):        at android.view.ViewGroup.measureChildWithMargin
s(ViewGroup.java:3023)
E/AndroidRuntime(  914):        at android.widget.LinearLayout.measureChildBefor
eLayout(LinearLayout.java:888)
E/AndroidRuntime(  914):        at android.widget.LinearLayout.measureVertical(L
inearLayout.java:350)
E/AndroidRuntime(  914):        at android.widget.LinearLayout.onMeasure(LinearL
ayout.java:278)
E/AndroidRuntime(  914):        at android.view.View.measure(View.java:7964)
E/AndroidRuntime(  914):        at android.view.ViewGroup.measureChildWithMargin
s(ViewGroup.java:3023)
E/AndroidRuntime(  914):        at android.widget.FrameLayout.onMeasure(FrameLay
out.java:245)
E/AndroidRuntime(  914):        at android.view.View.measure(View.java:7964)
E/AndroidRuntime(  914):        at android.widget.LinearLayout.measureVertical(L
inearLayout.java:464)
E/AndroidRuntime(  914):        at android.widget.LinearLayout.onMeasure(LinearL
ayout.java:278)
E/AndroidRuntime(  914):        at android.view.View.measure(View.java:7964)
E/AndroidRuntime(  914):        at android.view.ViewGroup.measureChildWithMargin
s(ViewGroup.java:3023)
E/AndroidRuntime(  914):        at android.widget.FrameLayout.onMeasure(FrameLay
out.java:245)
E/AndroidRuntime(  914):        at android.view.View.measure(View.java:7964)
E/AndroidRuntime(  914):        at android.view.ViewRoot.performTraversals(ViewR
oot.java:763)
E/AndroidRuntime(  914):        at android.view.ViewRoot.handleMessage(ViewRoot.
java:1633)
E/AndroidRuntime(  914):        at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime(  914):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  914):        at android.app.ActivityThread.main(ActivityThrea
d.java:4363)
E/AndroidRuntime(  914):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(  914):        at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime(  914):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(  914):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:618)
E/AndroidRuntime(  914):        at dalvik.system.NativeStart.main(Native Method)

I/Process (   52): Sending signal. PID: 914 SIG: 3
I/dalvikvm(  914): threadid=7: reacting to signal 3
E/dalvikvm(  914): Unable to open stack trace file '/data/anr/traces.txt': Permi
ssion denied
I/Process (  914): Sending signal. PID: 914 SIG: 9
I/WindowManager(   52): WIN DEATH: Window{44da9a90 com.list/com.list.ListActivit
y paused=false}
I/ActivityManager(   52): Process com.list (pid 914) has died.
I/UsageStats(   52): Unexpected resume of com.android.launcher while already res
umed in com.list
E/gralloc (   52): [unregister] handle 0x47bf40 still locked (state=40000001)
W/InputManagerService(   52): Got RemoteException sending setActive(false) notif
ication to pid 914 uid 10030
D/dalvikvm(  183): GC freed 28 objects / 1360 bytes in 149ms
4

2 回答 2

1

看看我在这里找到了什么。尝试String[] values在链接中完成初始化并告诉我们结果。

于 2012-05-15T08:32:42.967 回答
1

这是一个非常愚蠢的错误。我没有i在 showList() 函数的 for 循环中增加值。只是添加i++工作。很抱歉浪费您的时间。

于 2012-05-15T08:50:42.910 回答