2015-09-28 61 views
-1

我是新来的,我正在做一些调试。我有一些困难,我有一些困难传达。getReadableDatabase()'空对象引用

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

我现在的问题似乎是:

SQLiteDatabase db = myOpenHelper.getReadableDatabase(); 

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

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 

你怎么想:

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的错误(提取物)?

+0

您可以发送完整的代码? – USKMobility

+0

你是否实例化myOpenHelper? –

+0

是的,先生,编辑,请告知,如果你想看到其他类? – Adamc79

回答

1

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

CustomOpenHelper myOpenHelper = new CustomOpenHelper(this); 

然后写出下面的语句,

SQLiteDatabase db = myOpenHelper.getReadableDatabase(); 
+0

谢谢你们,感谢Ganpat Kaliya。它花了我一段时间,但已经奏效。 – Adamc79