エミュレーターに、アプリケーションが予期せず停止したというメッセージが表示されます。コードでエラーを見つけることができません。
これは私のメインクラスDatabase2Activity
です:
package data.base;
import com.example.phone_no.DBhelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Database2Activity extends Activity {
EditText editText;
DBhelper DB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
DB = new DBhelper(Database2Activity.this);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String rr = editText.getText().toString();
DB.open();
DB.adddata("A", rr, "122");
DB.getAlldata();
DB.close();
}
});
}
}
そしてこれは私の2番目のJavaクラスDBhelper
です:
package com.example.phone_no;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBhelper {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "names";
public static final String KEY_PHONE = "phoneno";
private final Context ourContext;
private static final String DATABASE_TABLE = "Contactinfo";
private static final int DATABASE_VERSION = 27;
private static final String DATABASE_NAME = "contactdata.db";
private DbHelper ourHelper;
private SQLiteDatabase ourDatabase;
// end for location
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/*db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT NULL , "
+ KEY_PHONE + " INTEGER NULL);");*/
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER , " + KEY_NAME + " TEXT NULL , "
+ KEY_PHONE + " INTEGER NULL);");
// string value
String y = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY , " + KEY_NAME + " TEXT NULL , "
+ KEY_PHONE + " INTEGER NULL);";
System.out.println("query" + y);
Log.d("query", y);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DBhelper(Context c) {
ourContext = c;
}
public DBhelper open() throws SQLException
{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long adddata(String id,String name,String number)
{
// TODO Auto-generated method stub
// add the custom Image Gallery Image Path to Data Base
ContentValues cv = new ContentValues();
cv.put(KEY_ID, id);
cv.put(KEY_NAME, name);
cv.put(KEY_PHONE, number);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public void getAlldata()
{
Cursor details = null;
if (ourDatabase.isOpen() == false)
ourDatabase = ourHelper.getWritableDatabase();
if (ourDatabase.isOpen())
{
details = ourDatabase.query(DATABASE_TABLE, null, null, null, null, null, null);
for(details.moveToFirst();!details.isAfterLast();details.moveToNext())
{
String a=details.getString(0);
String b=details.getString(1);
String c=details.getString(2);
System.out.println("id--"+a+"name"+b+"phoneno"+c);
}
}
}
public long delete_image(String id)
{
if (ourDatabase.isOpen() == false)
ourDatabase = ourHelper.getWritableDatabase();
if (ourDatabase.isOpen())
{
return ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id, null);
}
return 0;
}
}
そしてこれはlogcatウィンドウのエラーメッセージです:
03-11 23:20:32.677: E/AndroidRuntime(444): FATAL EXCEPTION: main
03-11 23:20:32.677: E/AndroidRuntime(444): java.lang.NullPointerException
03-11 23:20:32.677: E/AndroidRuntime(444): at data.base.Database2Activity$1.onClick(Database2Activity.java:27)
03-11 23:20:32.677: E/AndroidRuntime(444): at android.view.View.performClick(View.java:2408)
03-11 23:20:32.677: E/AndroidRuntime(444): at android.view.View$PerformClick.run(View.java:8816)
03-11 23:20:32.677: E/AndroidRuntime(444): at android.os.Handler.handleCallback(Handler.java:587)
03-11 23:20:32.677: E/AndroidRuntime(444): at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 23:20:32.677: E/AndroidRuntime(444): at android.os.Looper.loop(Looper.java:123)
03-11 23:20:32.677: E/AndroidRuntime(444): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-11 23:20:32.677: E/AndroidRuntime(444): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 23:20:32.677: E/AndroidRuntime(444): at java.lang.reflect.Method.invoke(Method.java:521)
03-11 23:20:32.677: E/AndroidRuntime(444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-11 23:20:32.677: E/AndroidRuntime(444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-11 23:20:32.677: E/AndroidRuntime(444): at dalvik.system.NativeStart.main(Native Method)