更新 4
这是我的 DBhelper 类
package com.example.dbtest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBhelper extends SQLiteOpenHelper {
String tablename;
Cursor ch;
private static String DB_PATH = "/data/data/com.example.dbtest/databases/";
public static String DB_NAME = "photodb";
private SQLiteDatabase myDataBase;
private Context myContext;
public DBhelper(Context cont) {
super(cont, DB_NAME, null, 3);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
boolean flag = isOpen();
if(flag==false){
//this.getWritableDatabase();
this.getReadableDatabase();
//myDataBase = SQLiteDatabase.openDatabase(DB_PATH,null, SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.NO_LOCALIZED_COLLATORS);
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
}
public boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput;
myInput = myContext.getAssets().open("photodb");
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput;
myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public boolean isOpen() {
if (myDataBase != null)
return myDataBase.isOpen();
return false;
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void add_pics(String imagepath,String description,String location)
{
myDataBase.execSQL("insert into photodetails values(null," + imagepath + ","
+ description + "," + location + ");");
}
}
这是我的 MainActivity
package com.example.dbtest;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
public class MainActivity extends Activity {
DBhelper mydb;
Context cont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBhelper(cont);
try {
mydb.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mydb.openDataBase();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的 logcat 错误是
09-02 18:43:26.052: E/AndroidRuntime(25477): 致命异常: main 09-02 18:43:26.052: E/AndroidRuntime(25477): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.dbtest/com.example.dbtest.MainActivity}:java.lang.NullPointerException 09-02 18:43:26.052:E/AndroidRuntime(25477):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 09 -02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app .ActivityThread.access$700(ActivityThread.java:140) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 09-02 18: 43:26.052: E/AndroidRuntime(25477): 在 android.os.Handler.dispatchMessage(Handler.java:99) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.os.Looper.loop(Looper.java:137) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.main (ActivityThread.java:4921) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-02 18:43:26.052: E/AndroidRuntime( 25477): 在 java.lang.reflect.Method.invoke(Method.java:511) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1027) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 09-02 18:43:26.052 : E/AndroidRuntime(25477): at dalvik.system.NativeStart.main(Native Method) 09-02 18:43:26.052: E/AndroidRuntime(25477): 引起:java.lang.NullPointerException 09-02 18:43 :26.052:E/AndroidRuntime(25477): 在 android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.database.sqlite.SQLiteOpenHelper.getReadableDatabase (SQLiteOpenHelper.java:188) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.DBhelper.createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E /AndroidRuntime(25477): 在 com.example.dbtest.MainActivity.onCreate(MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Activity.performCreate(Activity. java:5206) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-02 18:43:26.052: E/AndroidRuntime(25477) :在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)在 android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.DBhelper.createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在com.example.dbtest.MainActivity.onCreate(MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread .performLaunchActivity(ActivityThread.java:2074)在 android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.DBhelper.createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在com.example.dbtest.MainActivity.onCreate(MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread .performLaunchActivity(ActivityThread.java:2074)getDatabaseLocked(SQLiteOpenHelper.java:224) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.DBhelper.createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.MainActivity.onCreate (MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime (25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)getDatabaseLocked(SQLiteOpenHelper.java:224) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.DBhelper.createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.MainActivity.onCreate (MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime (25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)在 android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.DBhelper.createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.MainActivity.onCreate(MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-02 18: 43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)在 android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.DBhelper.createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.MainActivity.onCreate(MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-02 18: 43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.MainActivity.onCreate(MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java :1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)createDataBase(DBhelper.java:41) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 com.example.dbtest.MainActivity.onCreate(MainActivity.java:25) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Activity.performCreate(Activity.java:5206) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java :1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)java:1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)java:1094) 09-02 18:43:26.052: E/AndroidRuntime(25477): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)