我一直在搞乱 SimpleCursorAdapter 和 Listview 并得到不同的结果。起初我在我的列表视图上得到了一些结果,但是布局并不好,所以我决定修改它。然而,这似乎毁了一切,现在 APP 不断崩溃并出现空指针异常类型错误,我似乎无法弄清楚。
我的列表视图的 XML
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
我的活动课与 ListActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playerdata);
//list = (ListView) findViewById(R.id.list);
mDbHelper = new PlayerDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor players = mDbHelper.fetchAllPlayers();
startManagingCursor(players);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{PlayerDbAdapter.KEY_TITLE,PlayerDbAdapter.KEY_BODY};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.playerPosition,R.id.playerName};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter playerlist =
new SimpleCursorAdapter(this, R.layout.playerinfo , players, from, to);
setListAdapter(playerlist);
}
日志猫
04-15 20:09:07.326: E/AndroidRuntime(600): FATAL EXCEPTION: main
04-15 20:09:07.326: E/AndroidRuntime(600): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{your.dissertation.project/your.dissertation.project.PlayersActivity}: java.lang.NullPointerException
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.os.Looper.loop(Looper.java:123)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ActivityThread.main(ActivityThread.java:3647)
04-15 20:09:07.326: E/AndroidRuntime(600): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 20:09:07.326: E/AndroidRuntime(600): at java.lang.reflect.Method.invoke(Method.java:507)
04-15 20:09:07.326: E/AndroidRuntime(600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-15 20:09:07.326: E/AndroidRuntime(600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-15 20:09:07.326: E/AndroidRuntime(600): at dalvik.system.NativeStart.main(Native Method)
04-15 20:09:07.326: E/AndroidRuntime(600): Caused by: java.lang.NullPointerException
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.Activity.setContentView(Activity.java:1657)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ListActivity.ensureList(ListActivity.java:312)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ListActivity.getListView(ListActivity.java:297)
04-15 20:09:07.326: E/AndroidRuntime(600): at your.dissertation.project.PlayersActivity.<init>(PlayersActivity.java:27)
04-15 20:09:07.326: E/AndroidRuntime(600): at java.lang.Class.newInstanceImpl(Native Method)
04-15 20:09:07.326: E/AndroidRuntime(600): at java.lang.Class.newInstance(Class.java:1409)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-15 20:09:07.326: E/AndroidRuntime(600): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
04-15 20:09:07.326: E/AndroidRuntime(600): ... 11 more
我的光标类
public Cursor fetchAllPlayers()
{
return this.mDb.query("players", new String[] { "_id", "Player_Name", "Player_Position" }, null, null, null, null, null);
}