弹出窗口android中的EditText上的addTextChangedListener()

时间:2014-11-10 09:45:23

标签: android nullpointerexception android-edittext popupwindow textwatcher

我正在尝试使用弹出窗口,我可以在此之间更改延迟,从按下按钮直到显示图像的位置。我已经读过edittext和textwatcher应该采用的技巧,但我不断获得NullPointerExeption。

这是您在设置中并尝试设置延迟的部分。

else if(id == R.id.action_settings){
        LayoutInflater layoutInflater  = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        View setpopView = layoutInflater.inflate(R.layout.setpop, null);
        final PopupWindow setpopWindow = new PopupWindow(setpopView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        setpopWindow.setFocusable(true);
        setpopWindow.update();

        EditText delayTime = (EditText) setpopView.findViewById(R.id.editText1);
        delayTime.setText("" + timerTime);
        timerTime = Integer.parseInt(delayTime.getText().toString());

        delayTime.addTextChangedListener(new TextWatcher() {

            EditText delayTime = (EditText) findViewById(R.id.editText1);

              public void afterTextChanged(Editable s) {
                    delayTime.setText("" + timerTime);
              }

              public void beforeTextChanged(CharSequence s, int  start, int count, int after) {
                    //TODO Auto-generated method stub
              }

              public void onTextChanged(CharSequence s, int start, int before, int count) {
                   //TODO Auto-generated method stub
              }
        });


        ((TextView)setpopWindow.getContentView().findViewById(R.id.textDelay2)).setText("Current delay: " + timerTime + "sec");


        Button btnDismiss = (Button)setpopView.findViewById(R.id.setDismiss);
        btnDismiss.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                //TODO Auto-generated method stub
                setpopWindow.dismiss();
            }
        });  
        setpopWindow.showAsDropDown(btnDismiss, 50, 50);
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="@android:color/black"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
    android:layout_width="230dp"
    android:layout_height="300dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_margin="2dp"
    android:background="@android:color/darker_gray"
    android:orientation="vertical" >

    <Button
        android:id="@+id/setDismiss"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Back" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="         Settings"
            android:textSize="22sp" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seekBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:text="" />

    <TextView
        android:id="@+id/textDelay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/relativeLayout1"
        android:layout_below="@+id/relativeLayout1"
        android:text="Set result delay (sec)" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textDelay"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="number"
        android:text=""  >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textDelay2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textDelay"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="16dp"
        android:text="" />

</RelativeLayout>

logcat的

11-10 03:30:00.776: E/AndroidRuntime(30751): FATAL EXCEPTION: main
11-10 03:30:00.776: E/AndroidRuntime(30751): Process: com.gerfort.gerfortrps, PID: 30751
11-10 03:30:00.776: E/AndroidRuntime(30751): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
11-10 03:30:00.776: E/AndroidRuntime(30751):    at com.gerfort.gerfortrps.MainActivity$14.afterTextChanged(MainActivity.java:413)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.widget.TextView.sendAfterTextChanged(TextView.java:7472)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9246)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:957)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:501)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:216)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:33)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:243)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:382)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.os.Looper.loop(Looper.java:135)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at android.app.ActivityThread.main(ActivityThread.java:5070)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at java.lang.reflect.Method.invoke(Native Method)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at java.lang.reflect.Method.invoke(Method.java:372)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
11-10 03:30:00.776: E/AndroidRuntime(30751):    at      com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)

当我尝试编辑文本时,它会一直崩溃。有任何想法吗?提前致谢!

4 个答案:

答案 0 :(得分:1)

试试这个

final EditText delayTime = (EditText) setpopView.findViewById(R.id.editText1);
delayTime.setText(String.valueOf(timerTime));
timerTime = Integer.parseInt(delayTime.getText().toString());

delayTime.addTextChangedListener(new TextWatcher() {

      public void afterTextChanged(Editable s) {
           if(!s.toString().trim().equalsIgnoreCase("")) {
               timerTime = Integer.parseInt(s.toString().trim());
               //delayTime.setText(String.valueOf(timerTime));
           } else {
               timerTime = 0;//set initial value of timerTime
               //delayTime.setText(String.valueOf(timerTime));
           }                
      }

      public void beforeTextChanged(CharSequence s, int  start, int count, int after) {
            //TODO Auto-generated method stub
      }

      public void onTextChanged(CharSequence s, int start, int before, int count) {
           //TODO Auto-generated method stub
      }
});

答案 1 :(得分:0)

有时,findViewById就像异步调用一样。我建议你改变

EditText delayTime = (EditText) setpopView.findViewById(R.id.editText1);

final EditText delayTime = (EditText) setpopView.findViewById(R.id.editText1);

并删除

EditText delayTime =(EditText)findViewById(R.id.editText1);

delayTime.addTextChangedListener(new TextWatcher() {

            **EditText delayTime = (EditText) findViewById(R.id.editText1);**

答案 2 :(得分:0)

您执行的每个输入都被“”和NullString替换。原因是

delayTime.setText("" + timerTime);

本身调用

 afterTextChanged(Editable s)

递归并用

替换EditText
"" + timerTime

为空。 所以不要调用setText(),但尝试类似

的东西
afterTextChanged(Editable s){
   char lastCharacter = s.charAt(s.length() - 1);
   if (lastCharacter == '\n') {
      timerTime = Integer.parseInt(s);
   }
}

'\ n'代表输入按钮。

答案 3 :(得分:0)

崩溃是因为你有一个无限循环:

  1. 您编辑EditText
  2. TextWatcher检测到EditText内容已更改(在步骤1中),因此它执行afterTextChanged
  3. TextWatcher检测到EditText内容已更改(在步骤2中),因此执行afterTextChanged
  4. TextWatcher检测到EditText内容已更改(在步骤3中),因此它执行afterTextChanged
  5. ......等等。

    你必须控制那个无限循环。最简单的方法是使用标志,如下所示:

    boolean disableTextWatcher;
    

        disableTextWatcher = false;
        delayTime.addTextChangedListener(new TextWatcher() {
    
                    EditText delayTime = (EditText) findViewById(R.id.editText1);
    
                      public void afterTextChanged(Editable s) {
                         if (!disableTextWatcher) {
                            disableTextWatcher = true;
                            delayTime.setText("" + timerTime);
                            disableTextWatcher = false;
                          }
                      }
    
                      public void beforeTextChanged(CharSequence s, int  start, int count, int after) {
                            //TODO Auto-generated method stub
                      }
    
                      public void onTextChanged(CharSequence s, int start, int before, int count) {
                           //TODO Auto-generated method stub
                      }
                });
    
相关问题