0

我的一个应用程序中有以下代码。我在此遇到运行时错误

public class MainActivity extends Activity {
    private SQLiteAdapter mySQLiteAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        ListView listContent = (ListView) findViewById(R.id.listView1);
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();
        Cursor cursor = mySQLiteAdapter.queueAll();
        startManagingCursor(cursor);
        String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
        int[] to = new int[] { R.id.textView1, R.id.textView2 };
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
                R.layout.row, cursor, from, to);
        listContent.setAdapter(cursorAdapter);
        mySQLiteAdapter.close();
    }
}

错误如下:

06-08 00:04:17.133: E/AndroidRuntime(7072): FATAL EXCEPTION: main
06-08 00:04:17.133: E/AndroidRuntime(7072): java.lang.RuntimeException: Unable to start activity ComponentInfo{bl.ock/bl.ock.MainActivity}: java.lang.NullPointerException
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.os.Looper.loop(Looper.java:130)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.app.ActivityThread.main(ActivityThread.java:3835)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at java.lang.reflect.Method.invoke(Method.java:507)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at dalvik.system.NativeStart.main(Native Method)
06-08 00:04:17.133: E/AndroidRuntime(7072): Caused by: java.lang.NullPointerException
06-08 00:04:17.133: E/AndroidRuntime(7072):     at bl.ock.MainActivity.onCreate(MainActivity.java:28)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-08 00:04:17.133: E/AndroidRuntime(7072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
06-08 00:04:17.133: E/AndroidRuntime(7072):     ... 11 more

请帮我解决这个问题。谢谢你。

4

1 回答 1

4

setContetView(<your_layout_id>) in missing in onCreate()使用 findViewById() 获取列表之前的函数

    super.onCreate(savedInstanceState);
     setContetView(<your_layout_id>);<------------------
    ListView listContent = (ListView) findViewById(R.id.listView1);
于 2012-06-07T18:41:49.540 回答