使用TextWatcher时出现NullPointerError

时间:2016-08-22 14:17:49

标签: android nullpointerexception textwatcher

我目前正在尝试将TextWatcher用于我的应用程序。

但是,我发现删除某个NullPointerError很困难,每当我发起关注的活动时就会发生这种错误。我试过使用stacktrace,但我无法解决它。你能帮我吗?

以下是我目前的代码:

public class TransEncrypt extends AppCompatActivity {

    List<String> Bucket = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trans_encrypt);

        EditText EncryptInput = (EditText) findViewById(R.id.enter_message_field);
        EncryptInput.addTextChangedListener(new TextWatcher() {
            TextView EncryptOutput = (TextView) findViewById(R.id.encrypted_output);
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                EncryptOutput.setText(R.string.default_encrypt);
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

}

这是我的堆栈跟踪:

08-22 23:47:16.434 29280-29280/com.example.denny.scrambler E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.denny.scrambler/com.example.denny.scrambler.TransEncrypt}: java.lang.NullPointerException
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                                                                                 at android.app.ActivityThread.access$700(ActivityThread.java:159)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                 at android.os.Looper.loop(Looper.java:137)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                                              Caused by: java.lang.NullPointerException
                                                                                 at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:72)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
                                                                                 at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
                                                                                 at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183)
                                                                                 at com.example.denny.scrambler.TransEncrypt$1.<init>(TransEncrypt.java:28)
                                                                                 at com.example.denny.scrambler.TransEncrypt.<init>(TransEncrypt.java:27)
                                                                                 at java.lang.Class.newInstanceImpl(Native Method)
                                                                                 at java.lang.Class.newInstance(Class.java:1130)
                                                                                 at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
                                                                                 at android.app.ActivityThread.access$700(ActivityThread.java:159) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                 at android.os.Looper.loop(Looper.java:137) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5419) 
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
                                                                                 at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

您的错误发生在匿名类中,因为您在其中调用findViewById的上下文不存在。

at com.example.denny.scrambler.TransEncrypt$1.<init>(TransEncrypt.java:28)

尝试移动这些线

EditText EncryptInput = (EditText) findViewById(R.id.enter_message_field);
EncryptInput.addTextChangedListener(new TextWatcher() {
    TextView EncryptOutput = (TextView) findViewById(R.id.encrypted_output);

喜欢这个

EditText EncryptInput = (EditText) findViewById(R.id.enter_message_field);
final TextView EncryptOutput = (TextView) findViewById(R.id.encrypted_output);
EncryptInput.addTextChangedListener(new TextWatcher() {

或者,更好的是,将这些视图作为类的字段。