NullPointerException - onPause()

时间:2012-01-04 14:20:29

标签: java android

这是非常简单的事情,我希望能够解决这个问题并且让我成为一个新手。

我的屏幕上有多个标签。以下代码应读取文本输入,并在选择另一个选项卡时将其值分配给“共享首选项”。但是,每当我更改为另一个选项卡时,我的代码都会因NullPointerException而失败 - 我相信我已将其跟踪到下面代码的onPause(),我相信这是因为我无法正确传递变量中的数据。

任何指示赞赏!

package com.androidbook.epcsn;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("unused")
public class jobActivity extends Activity {   

public static final String SN_PREFERENCES  = "SiteNotePrefs";

SharedPreferences mPrefSettings;
String jobID;
String jobAddress ;
String jobPostcode ;

public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);          
    setContentView(R.layout.joblayout); 

    final SharedPreferences mPrefSettings = getSharedPreferences (SN_PREFERENCES, Context.MODE_WORLD_WRITEABLE);

    initjobID();
    initjobaddress();
    initjobpostcode();
}

private void initjobpostcode() {
    EditText jobPostcode = (EditText)findViewById(R.id.jobPostcodeText);
}

private void initjobaddress() {
    EditText jobAddress = (EditText)findViewById(R.id.jobAddressText);
}

private void initjobID() {
    EditText jobID = (EditText)findViewById(R.id.jobIDText);
}

@Override
protected void onPause(){
    super.onPause();

    String strjobID = jobID;
    String strjobAddress = jobAddress;
    String strjobPostcode = jobPostcode;

    Editor editor = mPrefSettings.edit();
    editor.putString("jobID", strjobID);
    editor.putString("jobAddress", strjobAddress);
    editor.putString("jobPostcode", strjobPostcode);
    editor.commit();
}
}

3 个答案:

答案 0 :(得分:4)

您可以在init方法中将值设置为局部变量,而不是预期的类属性,例如:

EditText jobID;

// Then later... it's clobberin' time.
private void initjobID() {
    EditText jobID = (EditText)findViewById(R.id.jobIDText);
}

属性jobIDshadowed,是方法中声明的本地jobID

(Java约定将方法命名为initJobId,属性命名为jobId)。

要在init方法中设置实例属性,请删除本地声明并使用此模式:

EditText jobID;
private void initjobID() {
    jobID = (EditText)findViewById(R.id.jobIDText);
}

答案 1 :(得分:0)

我想是因为你正在设置

final SharedPreferences mPrefSettings = getSharedPreferences (SN_PREFERENCES, Context.MODE_WORLD_WRITEABLE);

到局部变量而不是类成员字段。

因此异常可能在:

mPrefSettings.edit();

因为它仍为空(从未在init方法中设置)。

尝试将其更改为:

mPrefSettings = getSharedPreferences (SN_PREFERENCES, Context.MODE_WORLD_WRITEABLE);

另外要小心你的其他初始化方法,例如initjobID,因为他们犯了同样的错误。

将您的课程更改为:

package com.androidbook.epcsn;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("unused")
public class jobActivity extends Activity {   

public static final String SN_PREFERENCES  = "SiteNotePrefs";

SharedPreferences mPrefSettings;
String jobID;
String jobAddress ;
String jobPostcode ;

public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);          
    setContentView(R.layout.joblayout); 

    mPrefSettings = getSharedPreferences (SN_PREFERENCES, Context.MODE_WORLD_WRITEABLE);

    initjobID();
    initjobaddress();
    initjobpostcode();
}

private void initjobpostcode() {
    jobPostcode = (EditText)findViewById(R.id.jobPostcodeText);
}

private void initjobaddress() {
    jobAddress = (EditText)findViewById(R.id.jobAddressText);
}

private void initjobID() {
    jobID = (EditText)findViewById(R.id.jobIDText);
}

@Override
protected void onPause(){
    super.onPause();

    String strjobID = jobID;
    String strjobAddress = jobAddress;
    String strjobPostcode = jobPostcode;

    Editor editor = mPrefSettings.edit();
    editor.putString("jobID", strjobID);
    editor.putString("jobAddress", strjobAddress);
    editor.putString("jobPostcode", strjobPostcode);
    editor.commit();
}
}

答案 2 :(得分:0)

您在onCreate()期间设置的变量与您在课程级别声明的变量不同。这实际上是对如何在Java中声明和修改变量内容的基本误解。请参阅以下链接,也许它会帮助您理解。

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html