onPause()NullPointerException - 尝试将数据写入数据库onPause

时间:2012-05-09 05:17:15

标签: java android

我正在尝试在暂停应用程序时将一些可变数据写回数据库表,以便以后检索。我承认在这一点上要签一个完整的菜鸟(我们都要从某个地方开始)。

我的代码:

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();


}


}

logcat的:

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%确定他们的工作,因为我能够发布和显示表中的数据。

1 个答案:

答案 0 :(得分:4)

实际上,您错过了初始化SQLiteAdapter jobSave类的实例。并直接使用其方法openToWrite()。因此,首先初始化jobSave实例,然后使用方法openToWrite()

修改

在您的活动的onCreate()代码行中

jobSave = new SQLiteAdapter(this);

现在再次运行您的代码,让我知道发生了什么......