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