嗨,斯塔克人……
我试图让我的应用程序保存带有EditText
框的文本,以便当用户关闭该应用程序并重新打开它时,输入字段仍会保存,但是单击按钮时却得到了NPE
。
请帮助我:)
package com.smarte.smartipcontrol;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class IPEntry extends Activity {
public final static String ACTUALSMARTIP = "com.smarte.smartipcontrol.ACTU_IP";
private EditText editText;
private EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_ipentry);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.act_ipentry, menu);
return true;
}
/** Called when the user clicks the SendIP button */
public void sendip(View view) {
Intent intent = new Intent(this, IPControl.class);
EditText editText = (EditText) findViewById(R.id.serverIpAddress);
String actu_ip = editText.getText().toString();
intent.putExtra(ACTUALSMARTIP, actu_ip);
startActivity(intent);
}
protected void onResume() {
super.onResume();
SharedPreferences prefs = getPreferences(0);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
editText.setText(restoredText, TextView.BufferType.EDITABLE);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
if (selectionStart != -1 && selectionEnd != -1) {
editText.setSelection(selectionStart, selectionEnd);
}
SharedPreferences prefs2 = getPreferences(1);
String restoredText2 = prefs2.getString("text2", null);
if (restoredText2 != null) {
editText2.setText(restoredText2, TextView.BufferType.EDITABLE);
int selectionStart2 = prefs2.getInt("selection-start2", -1);
int selectionEnd2 = prefs2.getInt("selection-end2", -1);
if (selectionStart2 != -1 && selectionEnd2 != -1) {
editText2.setSelection(selectionStart2, selectionEnd2);
}
}
}
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = getPreferences(0).edit();
editor.putString("text", editText.getText().toString());
editor.putInt("selection-start", editText.getSelectionStart());
editor.putInt("selection-end", editText.getSelectionEnd());
editor.commit();
SharedPreferences.Editor editor2 = getPreferences(1).edit();
editor2.putString("text2", editText2.getText().toString());
editor2.putInt("selection-start2", editText2.getSelectionStart());
editor2.putInt("selection-end2", editText2.getSelectionEnd());
editor2.commit();
}
}
LogCat
12-17 13:20:20.043: E/AndroidRuntime(1069): FATAL EXCEPTION: main
12-17 13:20:20.043: E/AndroidRuntime(1069): java.lang.RuntimeException: Unable to pause activity {com.smarte.smartipcontrol/com.smarte.smartipcontrol.IPEntry}: java.lang.NullPointerException
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3016)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.ActivityThread.access$800(ActivityThread.java:141)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.os.Looper.loop(Looper.java:137)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-17 13:20:20.043: E/AndroidRuntime(1069): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 13:20:20.043: E/AndroidRuntime(1069): at java.lang.reflect.Method.invoke(Method.java:511)
12-17 13:20:20.043: E/AndroidRuntime(1069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-17 13:20:20.043: E/AndroidRuntime(1069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-17 13:20:20.043: E/AndroidRuntime(1069): at dalvik.system.NativeStart.main(Native Method)
12-17 13:20:20.043: E/AndroidRuntime(1069): Caused by: java.lang.NullPointerException
12-17 13:20:20.043: E/AndroidRuntime(1069): at com.smarte.smartipcontrol.IPEntry.onPause(IPEntry.java:72)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.Activity.performPause(Activity.java:5206)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
12-17 13:20:20.043: E/AndroidRuntime(1069): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3002)
12-17 13:20:20.043: E/AndroidRuntime(1069): ... 12 more
最佳答案
我从来没有看到您实例化editText2
,一旦您检查了第72行:at com.smarte.smartipcontrol.IPEntry.onPause(IPEntry.java:72)
(也可以是editText
,请参见我的编辑),您可能会承认它。
由于editText
和editText2
(顺便说一句,名称选择不佳)是全局变量,因此应在onCreate()
中实例化它们:
editText = (EditText) findViewById(R.id.serverIpAddress);
editText2 = (EditText) findViewById(R.id.theSecondEditText);
编辑:顺便说一句,在
sendIp
中,您实例化了局部变量editText
,这意味着不仅editText2
是null
,editText
也是null
!永远不要让全局变量和局部变量具有相同的名称(除了构造函数,然后再使用this
关键字)。