2016-08-22 77 views
-2

我當前正在嘗試爲我的應用程序使用TextWatcher。使用TextWatcher時出現NullPointerError

但是,我發現在刪除某個NullPointerError時遇到困難,每當我發起關注活動時都會發生這種情況。我試圖使用堆棧跟蹤,但我無法修復它。你能幫我麼?

下面是我當前的代碼:

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)  

回答

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() { 

,或者甚至更好,使這些意見作爲類的字段。