NullPointerException in JdwpPaquet
I am trying to run the following Android app, but I开发者_如何学Go get a NullPointerException
Main.java
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Main extends ListActivity {
String[] listActivityNames =
{"Text1", "Text2", "Text3", "Text4"};
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use an existing ListAdapter that will map an array
// of strings to TextViews
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listActivityNames));
getListView().setTextFilterEnabled(true);
}
/*protected void onListItemClick(ListView l, View v, int position, long id) {
Intent myIntent = new Intent();
myIntent.setClassName("com.utsc", "com.utsc.Announcements");
startActivity(myIntent);
}*/
}
Console:
[2011-01-26 13:58:57 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:573)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:670)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
[2011-01-26 13:58:57 - ddms]null
java.lang.NullPointerException
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:573)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:670)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
I don't care that this question is really old, this was happening to me and it took me a while to figure out.
Make sure you don't have any xml files open anywhere. I closed all my xml files and this exception went away.
I tried running your app and it seems to run fine. Here is what I tested:
package com.stackoverflow.testlistactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class TestListActivity extends ListActivity {
String[] listActivityNames = {
"Text1", "Text2", "Text3", "Text4"
};
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use an existing ListAdapter that will map an array
// of strings to TextViews
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
listActivityNames));
getListView().setTextFilterEnabled(true);
}
}
The error is not completely usefull, I think there might be more hints in your error log, but still:
I think your getListView()
doesn't return an object. You might want to call setListAdapter()
first, before you can do stuff with the listViews, but i'm not completely sure why that doesn't work in your case. Could there be an error in that call?
edit: This seems to be in agreement with wat @noel just posted.
精彩评论