当我运行此代码时:
package com.herring.android.finalproject;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class GamesListActivity extends ListActivity {
private Cursor gamesCursor;
private GameDataBaseManager mDbHelper;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.toprated);
mDbHelper = new GameDataBaseManager(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
final ListView lv = (ListView) findViewById(R.id.text1);
lv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Intent i = new Intent(GamesListActivity.this, Game.class);
startActivityForResult(i, 0);
}
});
}
private void fillData()
{
gamesCursor = mDbHelper.getAllGames();
startManagingCursor(gamesCursor);
String[] from = new String[]{GameDataBaseManager.KEY_TITLE};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter games = new SimpleCursorAdapter(this, R.layout.toprateditem, gamesCursor, from, to);
setListAdapter(games);
}
}
我在Logcat中得到了这个:
01-18 17:46:08.742: ERROR/AndroidRuntime(1382): Uncaught handler: thread main exiting due to uncaught exception
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.herring.android.finalproject/com.herring.android.finalproject.GamesListActivity}: java.lang.NullPointerException
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.os.Looper.loop(Looper.java:123)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at java.lang.reflect.Method.invoke(Method.java:521)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at dalvik.system.NativeStart.main(Native Method)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): Caused by: java.lang.NullPointerException
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at com.herring.android.finalproject.GamesListActivity.onCreate(GamesListActivity.java:23)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
01-18 17:46:08.752: ERROR/AndroidRuntime(1382): ... 11 more
错误发生在lv.setOnClickListener 我到处寻找答案,但他们似乎都在谈论缺少setContentView,我不是。我完全迷失了。任何帮助将不胜感激。
编辑:这是我的toprated.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
答案 0 :(得分:1)
final ListView lv = (ListView) findViewById(R.id.text1);
而不是为什么不只是做getListView()?像下面这样的东西试试?
final ListView lv = getListView();
答案 1 :(得分:0)
使用ListActivity时,您必须设置ListView ID,如下所示:android:id="@+id/android:list"
答案 2 :(得分:0)
您将listView id设置为text1,但未在布局中声明它。您实现listactivity类,以便您可以通过这种方式调用listview
final ListView lv = getListView();
您已在Layout中声明了listview magic id,因此不需要使用findViewById()方法。您可以使用final ListView lv = getListView();
所以写下面的代码:
final ListView lv = getListView();
lv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Intent i = new Intent(GamesListActivity.this, Game.class);
startActivityForResult(i, 0);
}
});