更新 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): 致命的な例外: メイン 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(ネイティブ メソッド) 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): 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): at 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): at 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): at 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): at 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) で