尝试保存EditText时无法暂停

时间:2012-12-17 15:08:28

标签: java android nullpointerexception state

喜欢Stack的人...... 我正在尝试让我的应用程序使用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

2 个答案:

答案 0 :(得分:3)

我从来没有看到你实例化editText2,一旦你检查第72行,你就可能会得到认可:at com.smarte.smartipcontrol.IPEntry.onPause(IPEntry.java:72)(也可以是editText,看看我的编辑)。

由于editTexteditText2(顺便说一下名称选择不当)是全局变量,因此您应该在onCreate()中实例化它们:

editText = (EditText) findViewById(R.id.serverIpAddress);
editText2 = (EditText) findViewById(R.id.theSecondEditText);

修改:顺便说一下,在sendIp您要实例化本地变量editText,这意味着不仅editText2nulleditText也是null!您应 从不 同时具有相同名称的全局变量和局部变量(构造函数除外,然后使用this关键字)。

答案 1 :(得分:2)

我怀疑问题是你永远不会实例化editText2,所以当你尝试在其上调用getText()来检索要存储的值时它为空。

在一个不相关的注释中:editTexteditText2对于变量名称来说真的很糟糕。您应该始终努力使您的变量名称具有足够的描述性,以便您可以在不必寻找的情况下告诉它们是什么。也许你应该为其中一个考虑ipEntryTxt之类的东西?

编辑:还请注意您应该移动此行:

EditText editText = (EditText) findViewById(R.id.serverIpAddress);

在onCreate()内而不是在sendip()内。如果用户打开此活动但是从不按下按钮(调用sendip()),那么你的代码就是现在,那么editText将为null当onPause()触发时,它也会给你一个空指针异常。