アプリが一時停止したときに、後で取得するために、いくつかの変数データをデータベース テーブルに書き戻そうとしています。この時点で、私は完全な初心者であることを認めます (私たちは皆、どこかから始めなければなりません)。
私のコード:
package com.android.dbtest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import android.R.layout;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.InputEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
@SuppressWarnings("unused")
public class jobActivity extends Activity {
public static final String LOG_TAG = "dbtest";
public SQLiteAdapter jobSave;
//public String buttText;
//SimpleCursorAdapter cursorAdapter;
//String cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.joblayout);
final EditText jobIDInput = (EditText) findViewById(R.id.jobIDText);
final EditText jobAddress = (EditText) findViewById(R.id.jobAddressText);
final EditText jobPCInput = (EditText) findViewById(R.id.jobPostcodeText);
jobIDInput.setText(String.valueOf(Property.jobId));
jobAddress.setText(String.valueOf(Property.jobAddress));
jobPCInput.setText(String.valueOf(Property.jobPostCode));
Log.v(LOG_TAG, "Property part 2:" + Property.jobAddress);
jobIDInput.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable id) {
Property.jobId = jobIDInput.getText().toString();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
Property.jobId = jobIDInput.getText().toString();
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
jobAddress.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable id) {
Property.jobAddress = jobAddress.getText().toString();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
Property.jobAddress = jobAddress.getText().toString();
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
jobPCInput.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable id) {
Property.jobPostCode = jobPCInput.getText().toString();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
});
}
protected void onPause(){
Log.v(LOG_TAG, "job state called");
saveJobState();
super.onPause();
}
private void saveJobState() {
// TODO Auto-generated method stub
String id = Property.jobId;
String jobAddress = Property.jobAddress;
String jobPostCode = Property.jobPostCode;
Log.v(LOG_TAG, "Paused");
jobSave.openToWrite(); // This is line 388
Log.v(LOG_TAG, "open to write ok");
jobSave.insertjob(id, jobAddress, jobPostCode);
jobSave.close();
}
}
ログキャット:
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): FATAL EXCEPTION: main
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): java.lang.RuntimeException: Unable to pause activity {com.android.dbtest/com.android.dbtest.jobActivity}: java.lang.NullPointerException
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.LocalActivityManager.performPause(LocalActivityManager.java:200)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:184)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:288)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:691)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.widget.TabHost.setCurrentTab(TabHost.java:341)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:140)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.view.View.performClick(View.java:2538)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.view.View$PerformClick.run(View.java:9152)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.os.Handler.handleCallback(Handler.java:587)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.os.Looper.loop(Looper.java:130)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at dalvik.system.NativeStart.main(Native Method)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): Caused by: java.lang.NullPointerException
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at com.android.dbtest.jobActivity.saveJobState(jobActivity.java:388)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at com.android.dbtest.jobActivity.onPause(jobActivity.java:375)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.Activity.performPause(Activity.java:3877)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): ... 19 more
私の問題は、「jobsave.openToWrite()」が何も指していないことだと思いますが、SQLiteAdapter には間違いなくあります。
誰かが光を当てることができれば、私は非常に素晴らしいです!
編集: -OpenTo SQLiteAdapter からコードを書く 以下に追加。パッケージcom.android.dbtest;
import android.content.ContentValues;
import android.util.Log;
import android.widget.SimpleCursorAdapter;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c)
{
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException
{
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public class SQLiteHelper extends SQLiteOpenHelper
{
public SQLiteHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db)
{
Log.v (LOG_TAG, "Create " + SCRIPT_CREATE_DATABASE);
// TODO Auto-generated method stub
Log.v (LOG_TAG, "Create DB: ");
db.execSQL(SCRIPT_CREATE_DATABASE);
db.execSQL(SCRIPT_CREATE_COUNT);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(LOG_TAG,
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + MYDATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS Count");
onCreate(db);
}
public void onPause(){
updateAll();
}
}
}
投稿を大幅に短縮するために、データベース作成メソッドを切り捨てました。さらに、テーブルからデータを投稿および表示できるため、99.99% 確実に機能します。