0

挿入用と表示用の 2 つのボタンがあります。挿入は正常に機能しますが、表示ボタンをクリックするとアプリがクラッシュし、次のエラーが発生します

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;
    }

}
4

1 に答える 1

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;
}
于 2013-02-14T19:51:40.717 に答える