Android无法执行活动方法

时间:2014-08-02 04:02:43

标签: android sqlite

我正在尝试使用Android将记录插入到SQLite数据库中。这是我的按钮onClick时的代码,它将执行插入查询:

public void btnClick(View view) {
    String todayAsString = new SimpleDateFormat("yyyy-MM-dd HH:mm")
            .format(new Date());

    // Insert record into database
    DatabaseAdapter dbAdapt = new DatabaseAdapter(Exercise.this);
    dbAdapt.bookExercise(userName, exerciseIDList, todayAsString);

    Toast.makeText(getBaseContext(), "Show Results Clicked",
            Toast.LENGTH_SHORT).show();
    startActivity((new Intent(this.getBaseContext(), Results.class)));
}

在我的DatabaseAdapter中:

public class DatabaseAdapter {
protected static final String TAG = "DatabaseAdapter";
private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;

public DatabaseAdapter(Context context) {
    this.mContext = context;
    mDbHelper = new DataBaseHelper(mContext);
}

public DatabaseAdapter createDatabase() throws SQLException {
    try {
        mDbHelper.createDataBase();
        Log.e(TAG, "Database Created");
    } catch (IOException mIOException) {
        Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
        throw new Error("UnableToCreateDatabase");
    }
    return this;
}

public SQLiteDatabase open() throws SQLException {
    try {
        mDbHelper.openDataBase();
        mDbHelper.close();
        mDb = mDbHelper.getReadableDatabase();
        Log.e(TAG, "Database Open");
    } catch (SQLException mSQLException) {
        Log.e(TAG, "open >>" + mSQLException.toString());
        throw mSQLException;
    }
    return mDb;
}

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

public long bookExercise(String userName, ArrayList<String> exerciseIDList, String todayAsString) 
{
    ContentValues newEntryValues = new ContentValues();
    for (int i = 0; i < exerciseIDList.size(); i++) {
        try {
            newEntryValues.put("userName", userName);
            newEntryValues.put("exerciseID", exerciseIDList.get(i));
            newEntryValues.put("bookedTime", todayAsString);
        } catch (Exception ex) {
        }
    }
    return mDb.insert("bookedExercise", null, newEntryValues);
}

DatabaseHelper:

public class DataBaseHelper extends SQLiteOpenHelper {
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat
                                                // window
// destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME = "schoolAssignment";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, 1);// 1? its Database Version
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    this.mContext = context;
    try
    {   
        createDataBase();
    }
    catch (Exception e)
    {   
        Log.e(TAG,"DatabaseHelper_constuctor createDataBase :" + e.fillInStackTrace());   
    }
}

public void createDataBase() throws IOException {
    // If database not exists copy it from the assets
    Log.e(TAG, "CreateDataBase()");
    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        this.close();
        try {
            // Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } catch (IOException mIOException) {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

// Check that the database exists here: /data/data/your package/databases/Database
// Name
private boolean checkDataBase() {
    File dbFile = new File(DB_PATH + DB_NAME);
    Log.e("dbFile", dbFile + "   "+ dbFile.exists());
    return dbFile.exists();
}

// Copy the database from assets
private void copyDataBase() throws IOException {
    InputStream mInput = mContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer)) > 0) {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

// Open the database, so we can query it
public boolean openDataBase() throws SQLException {
    String mPath = DB_PATH + DB_NAME;
    // Log.v("mPath", mPath);
    mDataBase = SQLiteDatabase.openDatabase(mPath, null,
            SQLiteDatabase.CREATE_IF_NECESSARY);
    // mDataBase = SQLiteDatabase.openDatabase(mPath, null,
    // SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return mDataBase != null;
}

@Override
public synchronized void close() {
    if (mDataBase != null)
        mDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}

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

}

但是,我收到的错误消息是LogCat:

08-02 11:59:16.497: E/AndroidRuntime(24821): FATAL EXCEPTION: main
08-02 11:59:16.497: E/AndroidRuntime(24821): java.lang.IllegalStateException: Could not execute method of the activity
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.view.View$1.onClick(View.java:3098)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.view.View.performClick(View.java:3620)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.view.View$PerformClick.run(View.java:14292)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.os.Handler.handleCallback(Handler.java:605)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.os.Looper.loop(Looper.java:137)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.app.ActivityThread.main(ActivityThread.java:4512)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at java.lang.reflect.Method.invokeNative(Native Method)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at java.lang.reflect.Method.invoke(Method.java:511)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at dalvik.system.NativeStart.main(Native Method)
08-02 11:59:16.497: E/AndroidRuntime(24821): Caused by: java.lang.reflect.InvocationTargetException
08-02 11:59:16.497: E/AndroidRuntime(24821):    at java.lang.reflect.Method.invokeNative(Native Method)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at java.lang.reflect.Method.invoke(Method.java:511)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at android.view.View$1.onClick(View.java:3093)
08-02 11:59:16.497: E/AndroidRuntime(24821):    ... 11 more
08-02 11:59:16.497: E/AndroidRuntime(24821): Caused by: java.lang.NullPointerException
08-02 11:59:16.497: E/AndroidRuntime(24821):    at edu.nyp.project.DatabaseAdapter.bookExercise(DatabaseAdapter.java:62)
08-02 11:59:16.497: E/AndroidRuntime(24821):    at edu.nyp.project.Exercise.btnClick(Exercise.java:97)
08-02 11:59:16.497: E/AndroidRuntime(24821):    ... 14 more
08-02 11:59:48.208: I/Process(24821): Sending signal. PID: 24821 SIG: 9
08-02 11:59:48.521: D/dalvikvm(25205): GC_CONCURRENT freed 75K, 11% free 9489K/10567K, paused 5ms+2ms
08-02 11:59:48.536: D/dalvikvm(25205): GC_FOR_ALLOC freed 0K, 11% free 9489K/10567K, paused 15ms
08-02 11:59:48.536: I/dalvikvm-heap(25205): Grow heap (frag case) to 10.749MB for 642004-byte allocation
08-02 11:59:48.552: D/dalvikvm(25205): GC_FOR_ALLOC freed 1K, 10% free 10114K/11207K, paused 14ms
08-02 11:59:48.583: D/dalvikvm(25205): GC_CONCURRENT freed 279K, 13% free 9840K/11207K, paused 2ms+3ms
08-02 11:59:48.701: D/TextLayoutCache(25205): Using debug level: 0 - Debug Enabled: 0
08-02 11:59:48.763: D/dalvikvm(25205): GC_FOR_ALLOC freed 67K, 12% free 9910K/11207K, paused 15ms
08-02 11:59:48.763: I/dalvikvm-heap(25205): Grow heap (frag case) to 11.375MB for 868016-byte allocation
08-02 11:59:48.810: D/dalvikvm(25205): GC_CONCURRENT freed 3K, 12% free 10754K/12103K, paused 2ms+6ms
08-02 11:59:48.833: D/dalvikvm(25205): GC_FOR_ALLOC freed 0K, 12% free 10755K/12103K, paused 15ms
08-02 11:59:48.833: I/dalvikvm-heap(25205): Grow heap (frag case) to 13.236MB for 1953016-byte allocation
08-02 11:59:48.857: D/dalvikvm(25205): GC_FOR_ALLOC freed 0K, 10% free 12662K/14023K, paused 17ms
08-02 11:59:48.888: D/dalvikvm(25205): GC_CONCURRENT freed 0K, 10% free 12662K/14023K, paused 2ms+2ms
08-02 11:59:49.099: D/CLIPBOARD(25205): Hide Clipboard dialog at Starting input: finished by someone else... !
08-02 11:59:56.185: D/CLIPBOARD(25205): Hide Clipboard dialog at Starting input: finished by someone else... !

任何指南?提前谢谢。

1 个答案:

答案 0 :(得分:1)

可能mDb实例是null,因为在调用{方法open()方法之前调用insert method.Call bookExercise方法之前没有初始化为:DatabaseAdapter: / p>

DatabaseAdapter dbAdapt = new DatabaseAdapter(Exercise.this);
dbAdapt.open();  // open db connection here
dbAdapt.bookExercise(userName, exerciseIDList, todayAsString);
// close db connection here