简体   繁体   English

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

[英]onPause() NullPointerException - trying to write data to database onPause

I am trying to write some variable data back to a database table when the App is paused, for later retrieval. 我试图在App暂停时将一些变量数据写回到数据库表中,以供以后检索。 I will admit to beign a complete noob at this point (we all gotta start somewhere). 我承认这时将成为一个完整的菜鸟(我们都必须从某个地方开始)。

My Code: 我的代码:

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: 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

I suspect my problem is that 'jobsave.openToWrite()' is pointing to nothing, but it is defintely there in my SQLiteAdapter. 我怀疑我的问题是'jobsave.openToWrite()'没有指向任何东西,但是它确实存在于我的SQLiteAdapter中。

If anyone can shed any light, i;d be extremely greatful! 如果任何人都能见识到,我将非常感激!

EDIT: -OpenTo Write code from SQLiteAdapter Added below. 编辑:-OpenTo从SQLiteAdapter编写代码在下面添加。 package com.android.dbtest; 包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();
    }
}
}

I have chopped out the database create methods, for the sake of making post significantly shorter. 为了缩短发布时间,我已经砍掉了数据库的创建方法。 Plus i am 99.99% sure they work as i am able to post and show data from the table. 另外,我99.99%的人确信它们能正常工作,因为我能够张贴和显示表格中的数据。

Actually, You are missed to initialize SQLiteAdapter jobSave class's instance. 实际上,您错过了初始化SQLiteAdapter jobSave类的实例的操作。 And directly using its method openToWrite() . 并直接使用其方法openToWrite() So first initialize jobSave instance and then use the method openToWrite() . 因此,首先初始化jobSave实例,然后使用方法openToWrite()

EDIT: 编辑:

In your activity's onCreate() write code line, 在您活动的onCreate()编写代码行中,

jobSave = new SQLiteAdapter(this);

Now run again your code and let me know what happen.. 现在再次运行您的代码,让我知道会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM