这是我的代码:
这是我的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()函数时会抛出null ponter异常。有什么问题?
Logcat输出:
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
答案 0 :(得分:1)
看看我发现的here。尝试在链接中初始化您的String[] values
并告诉我们结果。
答案 1 :(得分:1)
这是一个非常愚蠢的错误。我没有在showList()函数中增加for循环中的i
值。只需添加i++
就可以了。抱歉浪费你的时间。