私のアプリケーションには MainActivity と Database Helpler クラスがあります。
主な活動
package com.example.sample;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
Button b1;
EditText et1;
protected DatabaseHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new DatabaseHelper(this);
b1=(Button)findViewById(R.id.button1);
et1=(EditText)findViewById(R.id.editText1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");
Date dateobject;
String date_var=(et1.getText().toString());
try {
dateobject=formatter.parse(date_var);
String date=new SimpleDateFormat().format(dateobject);
helper.AddDetail(date);
Toast.makeText(getApplicationContext(),""+date, Toast.LENGTH_SHORT).show();
helper.close();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
DatabaseHelper クラスは以下で定義されています
package com.example.sample;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper helper;
SQLiteDatabase db;
static final int VERSION=2;
static final String DATA_NAME="Mydata";
Context c;
public DatabaseHelper(Context context) {
super(context, DATA_NAME, null, VERSION);
c=context;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
String query="create table if not exists Mytable(id Integer primary key,Daate text
not null)";
db.execSQL(query);
}
public long AddDetail(String date)
{
db=getWritableDatabase();
ContentValues vals=new ContentValues();
vals.put("Daate",date);
long a=db.insert("Mytable", null, vals);
db.close();
return a;
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
行db.execSQL(query);にエラーが表示されています。データベース ヘルパー クラス (以下で定義) および行helper.AddDetail(date); で MainActivity 内。Logcat を以下に示します。
09-03 16:51:22.481: E/AndroidRuntime(3240): FATAL EXCEPTION: main
09-03 16:51:22.481: E/AndroidRuntime(3240): java.lang.NullPointerException
09-03 16:51:22.481: E/AndroidRuntime(3240): at com.example.sample.DatabaseHelper.onCreate(DatabaseHelper.java:27)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
09-03 16:51:22.481: E/AndroidRuntime(3240): at com.example.sample.DatabaseHelper.AddDetail(DatabaseHelper.java:31)
09-03 16:51:22.481: E/AndroidRuntime(3240): at com.example.sample.MainActivity$1.onClick(MainActivity.java:48)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.view.View.performClick(View.java:4204)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.view.View$PerformClick.run(View.java:17355)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.os.Handler.handleCallback(Handler.java:725)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.os.Looper.loop(Looper.java:137)
09-03 16:51:22.481: E/AndroidRuntime(3240): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-03 16:51:22.481: E/AndroidRuntime(3240): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 16:51:22.481: E/AndroidRuntime(3240): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 16:51:22.481: E/AndroidRuntime(3240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-03 16:51:22.481: E/AndroidRuntime(3240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-03 16:51:22.481: E/AndroidRuntime(3240): at dalvik.system.NativeStart.main(Native Method)