Android 由于NullPointerException,无法启动活动ComponentInfo

Android 由于NullPointerException,无法启动活动ComponentInfo,android,Android,当我运行此代码时: 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.widge

当我运行此代码时:

    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>

与其如此,为什么不直接执行getListView()?像下面这样的东西,试试看

final ListView lv = getListView();

使用ListActivity时,您必须设置ListView id,如下所示:
android:id=“@+id/android:list”
您将ListView id文本1放入布局中,但未将其声明。您可以实现listactivity类,以便通过这种方式调用listview

final ListView lv = getListView();
您已经在布局中声明了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);
                }
            });

如果
NullPointerException
打开
lv.setOnClickListener()
,则
lv
为空。它为null可能有两个原因:要么
findViewById()
找不到ID,要么
setContentView()
无法膨胀该XML元素。检查以确保您的所有id都是正确的(是
R.id.text1
ListView吗?),并再次检查XML本身。@斯蒂芬:您确定
R.id.text1
是正确的
id
?此外,如果是,则不应使用视图。单击Listener
时,应使用自动为
ListActivity
中的默认
ListView
设置的
ListActivity
@Rotemmiz:不正确。对于
ListActivity
,id必须是
android:id=“@android:id/list”
。如果你只是在正常的
活动中使用
列表视图
,你可以给它任何
id
,我指的是他的实现,但你是对的,我的公式化是错误的,改变了…:)@罗特米兹:是的,如果没有他的
tograted.xml
布局文件来查看,很难判断。
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);
                }
            });