0

嗨 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();
    }   
}

日志猫

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
4

2 回答 2

3

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

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

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

编辑:顺便说一句,sendIp你正在实例化一个局部变量editText,这意味着不仅editText2nulleditText也是null!您永远不应该同时拥有同名的全局变量和局部变量(构造函数除外,然后使用this关键字)。

于 2012-12-17T15:22:03.573 回答
2

我怀疑问题是您从未实例化editText2,因此当您尝试调用getText()它以检索要存储的值时它为空。

在一个不相关的注释上:editText并且editText2对于变量名来说真的是非常糟糕的选择。您应该始终努力使您的变量名称具有足够的描述性,以便您可以知道它们是什么而无需四处寻找。也许您应该考虑ipEntryTxt其中之一?

编辑:还请注意,您可能应该移动这一行:

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

在 onCreate() 内部而不是在内部sendip()因为您的代码现在是如果用户打开此活动但从未按下按钮(调用 sendip()),那么editText在 onPause() 触发时将为空,这将为您提供空指针异常也。

于 2012-12-17T15:12:41.837 回答