我有两个按钮,一个用于插入,一个用于显示。插入工作正常,但点击显示按钮后,我的应用程序崩溃,我得到以下错误
02-15 00:36:28.207: E/AndroidRuntime(25942): FATAL EXCEPTION: main
02-15 00:36:28.207: E/AndroidRuntime(25942): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.indianrailwaybooking/com.example.indianrailwaybooking.SQLView}: java.lang.NullPointerException
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.os.Looper.loop(Looper.java:123)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-15 00:36:28.207: E/AndroidRuntime(25942): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 00:36:28.207: E/AndroidRuntime(25942): at java.lang.reflect.Method.invoke(Method.java:507)
02-15 00:36:28.207: E/AndroidRuntime(25942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-15 00:36:28.207: E/AndroidRuntime(25942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-15 00:36:28.207: E/AndroidRuntime(25942): at dalvik.system.NativeStart.main(Native Method)
02-15 00:36:28.207: E/AndroidRuntime(25942): Caused by: java.lang.NullPointerException
02-15 00:36:28.207: E/AndroidRuntime(25942): at com.example.indianrailwaybooking.Reservation.getData(Reservation.java:96)
02-15 00:36:28.207: E/AndroidRuntime(25942): at com.example.indianrailwaybooking.SQLView.onCreate(SQLView.java:36)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-15 00:36:28.207: E/AndroidRuntime(25942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-15 00:36:28.207: E/AndroidRuntime(25942): ... 11 more
这是我的Cursor方法
public String getData() {
// TODO Auto-generated method stub
ourDatabase = ourHelper.getWritableDatabase();
String [] columns = new String[]{KEY_BOOKING, KEY_PERSON1, KEY_PERSON2, KEY_PERSON3, KEY_PERSON4};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null ,null);
String result = "";
ourDatabase.close();
int iBooking = c.getColumnIndex("KEY_BOOKING");
int iPerson1 = c.getColumnIndex("KEY_PERSON1");
int iPerson2 = c.getColumnIndex("KEY_PERSON2");
int iPerson3 = c.getColumnIndex("KEY_PERSON3");
int iPerson4 = c.getColumnIndex("KEY_PERSON4");
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iBooking) + " " + " " + c.getString(iPerson1) + " " + c.getString(iPerson2) + " " + c.getString(iPerson3) + " " + c.getString(iPerson4) + "\n";
}
Toast.makeText(context, result, Toast.LENGTH_LONG ).show();
return result;
}
这是调用getData方法的类。
public class SQLView extends Activity {
private DBHelper obj;
AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
Reservation resv = new Reservation(this);
String result = resv.getData();
EditText et = (EditText) findViewById(R.id.tvSQLinfo);
et.setText(result);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sqlview, menu);
return true;
}
}
答案 0 :(得分:0)
很难说没有看到更多代码,我会尝试将ourHelper.getWritableDatabase();
替换为ourHelper.getReadableDatabase();
并删除ourDatabase.close();
尝试:
public String getData() {
// TODO Auto-generated method stub
ourDatabase = ourHelper.getReadableDatabase();
String [] columns = new String[]{KEY_BOOKING, KEY_PERSON1, KEY_PERSON2, KEY_PERSON3, KEY_PERSON4};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null ,null);
String result = "";
int iBooking = c.getColumnIndex("KEY_BOOKING");
int iPerson1 = c.getColumnIndex("KEY_PERSON1");
int iPerson2 = c.getColumnIndex("KEY_PERSON2");
int iPerson3 = c.getColumnIndex("KEY_PERSON3");
int iPerson4 = c.getColumnIndex("KEY_PERSON4");
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iBooking) + " " + " " + c.getString(iPerson1) + " " + c.getString(iPerson2) + " " + c.getString(iPerson3) + " " + c.getString(iPerson4) + "\n";
}
Toast.makeText(context, result, Toast.LENGTH_LONG ).show();
return result;
}