0

例外の原因となるメソッドはinsertdetails().

私のコード:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AllLevels extends Activity implements OnClickListener {

    ImageView iv1, iv2, iv3, iv4, iv5, iv6, iv7, iv8, iv9, iv10, iv11, iv12;
    TextView tv;
    MediaPlayer mp;
    DBAdapter dbAdapter;
    DBHelper dbHelper;
    SQLiteDatabase db;
    long a;

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

        mp = MediaPlayer.create(this, R.raw.main);
        mp.start();
        mp.setLooping(true);
        Context context = this;

        final SharedPrefsHandler spf = new SharedPrefsHandler(context);
        int i = spf.loadPrefs();

        Toast.makeText(this, "Value from sharedpref"+i, Toast.LENGTH_SHORT).show();

        //creating database and entering default values

        dbHelper = new DBHelper(this);

        dbHelper.getWritableDatabase(); //Works fine till here, even database is generated.

//ここで問題があると思われます。これらの行を削除するとコードが実行され、これらの行が含まれているとデータベースが生成されません。

        dbAdapter.insertDetails(1, 1, 0, 0); 
        dbAdapter.insertDetails(2, 2, 0, 0);
        dbAdapter.insertDetails(3, 3, 0, 0);
        dbAdapter.insertDetails(4, 4, 0, 0);
        dbAdapter.insertDetails(5, 5, 0, 0);
        dbAdapter.insertDetails(6, 6, 0, 0);
        dbAdapter.insertDetails(7, 7, 0, 0);
        dbAdapter.insertDetails(8, 8, 0, 0);
        dbAdapter.insertDetails(9, 9, 0, 0);
        dbAdapter.insertDetails(10, 10, 0, 0);
        dbAdapter.insertDetails(11, 11, 0, 0);
        dbAdapter.insertDetails(12, 12, 0, 0);
}

package com.s.wrestlernicknames;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapter {
    SQLiteDatabase db;
    DBHelper dbHelper;
    Context context;


    public DBAdapter(Context c) {
        this.context = c;
    }

    public DBAdapter openToRead() {
        dbHelper = new DBHelper(context);
        db = dbHelper.getReadableDatabase();
        return this;
    }

    public DBAdapter openToWrite() {
        dbHelper = new DBHelper(context);
        db = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        db.close();
    }

    public long insertDetails(int id, int level, int totalTime, int score) {//Function that is being called to insert the values.
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBHelper.C_ID, id);
        contentValues.put(DBHelper.LEVEL, level);
        contentValues.put(DBHelper.TIME, totalTime);
        contentValues.put(DBHelper.SCORE, score);
        openToWrite();
        long val = db.insert(DBHelper.TABLE_NAME, null, contentValues);
        close();
        return val;
    }

    public Cursor queryName() {
        String[] column = {DBHelper.C_ID, DBHelper.LEVEL, DBHelper.TIME, DBHelper.SCORE};
        openToWrite();
        Cursor cursor = db.query(DBHelper.TABLE_NAME, column, null, null, null, null, null);
        return cursor;
    }

    public long updateDetails(int id, int level, int totalTime, int score) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBHelper.C_ID, id);
        contentValues.put(DBHelper.LEVEL, level);
        contentValues.put(DBHelper.TIME, totalTime);
        contentValues.put(DBHelper.SCORE, score);
        openToWrite();
        long val = db.update(DBHelper.TABLE_NAME, contentValues, DBHelper.C_ID+"="+id, null);
        close();
        return val;
    }
}

データベースを生成するクラス:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "data";
    public static final String TABLE_NAME = "score_table";
    public static final String C_ID = "_id";
    public static final String LEVEL = "level";
    public static final String SCORE = "score";
    public static final String TIME = "time";
    public static final int VERSION = 1;

    private final String createDB = "create table if not exists "+ TABLE_NAME + "("
            + C_ID + " integer primary key, "
            + LEVEL + " integer, "
            + SCORE + " integer, "
            + TIME + " integer ); ";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(createDB);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("drop table "+ TABLE_NAME);
    }

}

スタックトレース:

07-01 01:01:28.429: E/AndroidRuntime(1193): FATAL EXCEPTION: main
07-01 01:01:28.429: E/AndroidRuntime(1193): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.s.wrestlernicknames/com.s.wrestlernicknames.AllLevels}: java.lang.NullPointerException
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.os.Looper.loop(Looper.java:137)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.ActivityThread.main(ActivityThread.java:4340)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at java.lang.reflect.Method.invoke(Method.java:511)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at dalvik.system.NativeStart.main(Native Method)
07-01 01:01:28.429: E/AndroidRuntime(1193): Caused by: java.lang.NullPointerException
07-01 01:01:28.429: E/AndroidRuntime(1193):     at com.s.wrestlernicknames.AllLevels.onCreate(AllLevels.java:53)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.Activity.performCreate(Activity.java:4465)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-01 01:01:28.429: E/AndroidRuntime(1193):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-01 01:01:28.429: E/AndroidRuntime(1193):     ... 11 more
07-01 01:01:31.259: I/Process(1193): Sending signal. PID: 1193 SIG: 9

close() ステートメント、構文、構造化など、多くのことを確認しました。助けていただければ幸いです。私はいくつかの基本的なエラーを犯していることを知っていますが、見つけることができませんでした。

4

2 に答える 2

0

何が問題なのかわかりませんでした。これがうまくいかない場合は、LogCat レジストリを配置することをお勧めします。

    private final String createDB = "create table if not exists "+ TABLE_NAME + "("
        + C_ID + " integer primary key, "
        + LEVEL + " integer, "
        + SCORE + " integer, "
        + TIME + " integer ); ";

に変更します:

     private final String createDB = "create table if not exists "+ TABLE_NAME + " ("
        + C_ID + " integer primary key, "
        + LEVEL + " integer, "
        + SCORE + " integer, "
        + TIME + " integer " + ");");
于 2013-06-30T20:17:42.127 に答える