2011-07-15 48 views
0

我輸入一些數據到這個數據庫,然後試圖顯示那些數據,但我得到Null Pointer Exception at getReadableDatabase()。從我進入數據的主要活動如下:getReadableDatabase()空指針異常

public class TabListData extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button b = (Button) findViewById(R.id.enter); 
     b.setOnClickListener(this); 
     Button b1 = (Button) findViewById(R.id.employeeslist); 
     b1.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 

     EditText name = (EditText) findViewById(R.id.name); 
     EditText mobile = (EditText) findViewById(R.id.mobile); 

     String nam = name.getText().toString(); 
     String mob = mobile.getText().toString(); 

     Button b = (Button) v; 
     Button b1 = (Button) v; 
     Database d = new Database(getBaseContext()); 

     switch (b.getId()) { 
     case R.id.enter: 
      d.input(nam, mob); 
     case R.id.employeeslist: 
      /* ArrayList<EmployeesResult> results = d.selectEmployees(); 
       Toast t = Toast.makeText(getBaseContext(), "# records: " 
         + results.size(), 1000); 
       t.show();*/ 
      Intent i = new Intent(this, EmployeesList.class); 
      // i.putExtra("Name", "Name"); 
      startActivity(i); 
     } 
     } 
    } 

這是我的助手類

public class BaseHelper extends SQLiteOpenHelper { 

    public BaseHelper(Context context) { 
     super(context, "Employees.db", null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     String createtable = "create table Employee " + "(Name TEXT, " + "Mobile TEXT)"; 
     db.execSQL(createtable); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
    } 
} 

這是我的數據庫類。我用它來輸入和數據檢索。當selectEmployees()方法不存在但現在顯示Null Pointer Exception時,它的輸入工作正常。

public class Database { 

    private BaseHelper helper; 
    SQLiteDatabase db; 
    public Database(Context c){ 
     helper = new BaseHelper(c); 
    } 

    public void input(String Name, String Mobile){ 
     db = helper.getWritableDatabase(); 
     try{ 
     ContentValues values = new ContentValues(); 
     values.put("Mobile", Mobile); 
     values.put("Name", Name); 
     db.insert("Employee", "", values); 
     Log.e("ENTERED", "ENTERED "+values); 
     }finally { 
      if (db != null) 
       db.close(); 
     } 
    } 
    public ArrayList<EmployeesResult> selectEmployees() { 

     db = helper.getReadableDatabase(); 
     try{ 
     ArrayList<EmployeesResult> results = new ArrayList<EmployeesResult>(); 
     Cursor c = db.rawQuery("select * from Employee", null); 

     if (c.getCount() > 0) { 
       c.moveToFirst(); 
       do { 
        results.add(new EmployeesResult(
          c.getString(c.getColumnIndex("Name")))); 
          //c.getInt(c.getColumnIndex("Mobile")))); 
       }while(c.moveToNext()); 
     } 
     return results; 
    }finally { 
     if (db != null) 
      db.close(); 
    } 
    } 
} 

EmployeeResult類:

public class EmployeesResult { 
    public EmployeesResult(String Name){ 
     name = Name; 
     //mobile = Mobile; 
    } 
    public String name; 
    public int mobile; 
} 

這是logcat的

07-15 23:44:11.008: ERROR/AndroidRuntime(292): FATAL EXCEPTION: main 
07-15 23:44:11.008: ERROR/AndroidRuntime(292): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TabListData/com.TabListData.EmployeesList}: java.lang.NullPointerException 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.os.Looper.loop(Looper.java:123) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at dalvik.system.NativeStart.main(Native Method) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292): Caused by: java.lang.NullPointerException 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at com.TabListData.Database.selectEmployees(Database.java:35) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at com.TabListData.EmployeesList.onCreate(EmployeesList.java:30) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
07-15 23:44:11.008: ERROR/AndroidRuntime(292):  ... 11 more 
+0

有一些非常奇怪的事情縮進和括號中的代碼事情引起的。您可能想要檢查格式是否在您複製時破壞。 – thegrinner

+0

發佈logcat .. –

+0

另外,什麼是拋出錯誤? – thegrinner

回答

1

我的問題解決了,我發現那裏的問題正是問題是ListActivity和EmployeeResult類

對於ListActivity,只顯示文本的XML佈局文字的ListView 喜歡這個

<?xml version="1.0" encoding="UTF-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="fill_parent"  
android:padding="10dp"  
android:textSize="16sp" > 
</TextView> 

,並在ListActivity,你只需要設置一個ListAdapter

像這樣的onCreate()方法

List<String> data = dh.SelectStudent(); 
setListAdapter(new ArrayAdapter<String>(ListofStudents.this, R.layout.text, data)); 

和ArrayAdapter insted的EmployeesResult類取字符串和列表

DataBase類中的方法應該是

public ArrayList<String> SelectStudent() { 
    SQLiteDatabase db = _dbHelper.getReadableDatabase(); 
    try { 
     ArrayList<String> results = new ArrayList<String>(); 
     Cursor c = db.rawQuery("select * from Student", null); 
     if (c.getCount() > 0) { 
      c.moveToFirst(); 
      do { 
       results.add(c.getString(1)); 
       // results.add(c.getString(4)); 

      } while (c.moveToNext()); 
     } 
     return results; 
    } finally { 
     if (db != null) 
      db.close(); 
    } 
} 

我已經在列表和ArrayAdepter中傳遞了字符串,如果你需要更多的一個字符串,如名稱和手機號碼或字符串和圖像的組合,在這種情況下,你需要做一個單獨的類你有圖像和字符串,你需要傳遞該類而不是List和ArrayAdepter中的字符串。

THANX

+6

我遇到了和你一樣的問題。但我不明白你的修正。 NullPointerException拋出'SQLiteDatabase db = _dbHelper.getReadableDatabase();',你沒有改變這一行,但爲什麼它是固定的?謝謝! – Dagang

1

的錯誤可能是由一個空指針在上下文

public class SqLite extends SQLiteOpenHelper { 
    public SqLite(Context context, String table, boolean readOnly) { 
     super(context, DB_NAME, null, 1); 
     getReadableDatabase();