getReadableDatabase()'在null对象引用上

时间:2015-09-28 11:08:19

标签: java android listview android-sqlite

我是新手,我正在做一些调试。我遇到了一些困难,我在传达方面遇到了一些困难。

我试图从我在另一个活动中创建的SQLite数据库中读取。

我目前的问题似乎是:

SQLiteDatabase db = myOpenHelper.getReadableDatabase();

应指向我的数据库助手类。

public class SecondActivity extends ListActivity {

    CustomOpenHelper myOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        displayDataInTable();

    }

        void displayDataInTable() {
        List<String> values = queryTable();

        if (values != null) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(SecondActivity.this, android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);


        }

    }

    List<String> queryTable() {
        List<String> player = new ArrayList<String>();

        SQLiteDatabase db = myOpenHelper.getReadableDatabase();
        Cursor cursor = db.query(true, CustomOpenHelper.TABLE_NAME, new String[]{"_id, name, score"}, null, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int score = cursor.getInt(cursor.getColumnIndex("score"));
            player.add(id + " --> the player " + name + " has got a score of " + score + "s");
        }

        return player;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

LogCat错误(摘录):

09-28 20:41:57.039    1271-1285/system_process W/ActivityManager﹕ Launch timeout has expired, giving up wake lock!
09-28 20:42:23.468    3642-3642/com.example.eagle.sqlite2activitytest D/AndroidRuntime﹕ Shutting down VM
09-28 20:42:23.486    3642-3642/com.example.eagle.sqlite2activitytest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.eagle.sqlite2activitytest, PID: 3642
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eagle.sqlite2activitytest/com.example.eagle.sqlite2activitytest.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.eagle.sqlite2activitytest.CustomOpenHelper.getReadableDatabase()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.eagle.sqlite2activitytest.CustomOpenHelper.getReadableDatabase()' on a null object reference
            at com.example.eagle.sqlite2activitytest.SecondActivity.queryTable(SecondActivity.java:43)
            at com.example.eagle.sqlite2activitytest.SecondActivity.displayDataInTable(SecondActivity.java:29)
            at com.example.eagle.sqlite2activitytest.SecondActivity.onCreate(SecondActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-28 20:42:23.493    1271-1721/system_process W/ActivityManager﹕ Force finishing activity com.example.eagle.sqlite2activitytest/.SecondActivity

您怎么看?

2 个答案:

答案 0 :(得分:1)

试试这个你可以像这样创建适当的对象。

CustomOpenHelper myOpenHelper = new CustomOpenHelper(this);

然后写下面的语句,

SQLiteDatabase db = myOpenHelper.getReadableDatabase();

答案 1 :(得分:0)

无需致电:

SQLiteDatabase db = myOpenHelper.getReadableDatabase();

错误仅在此使用,因此您不需要sqlitedatabase对象:

Cursor cursor = getActivity().getContentResolver().query(Uri, 
projection,selection,selectionArgs,sortOrder);