Android:带有 EditText 的 onTextChanged() 方法中的 NullPointerException

Android: NullPointerException in onTextChanged() method with EditText

你好Android开发者,

当 EditText 更改其文本时,我的 onTextChanged() 方法中不断出现 NullPointerException。 并非在所有智能手机上。 LG G2 不会抛出任何错误,但三星 Galaxy S 4 会抛出任何错误。我有两个 EditText。通过输入数字,程序会获取两个值并计算出一些东西。真正奇怪的是,当键盘打开时,我按任意键,然后按 DEL 键,一切正常。

这里是一些代码:

EditText bestellt = new EditText(this);
bestellt.setInputType(InputType.TYPE_CLASS_NUMBER);
bestellt.setGravity(Gravity.CENTER_HORIZONTAL);
bestellt.setEms(3);
bestellt.setTextAppearance(this, android.R.style.TextAppearance_Medium);
bestellt.setBackgroundColor(Color.parseColor("#DBF2FC"));
bestellt.setHint(Html.fromHtml("<small><small><small>" +
                "Bestellt" + "</small></small></small>"));

//bestellt.setOverScrollMode(View.OVER_SCROLL_NEVER);
bestellt.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_FLAG_NAVIGATE_NEXT);
tempTableRow.addView(bestellt);
bestellt.setId(Integer.parseInt(Integer.toString(runde) + "90" + Integer.toString(i + 1) + "90" + Integer.toString(viewTeilID)));

//wieVieleReihen1 = tabellenLayout.getChildCount() - 1;
//scrollViewHor.setSmoothScrollingEnabled(false);

bestellt.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        viewEditText1 = v;
         /*if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {

               //Toast.makeText(getBaseContext(), "Done gedrueckt", Toast.LENGTH_SHORT).show();
               return true;
          }*/
         return false;
        }
    });

bestellt.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

     //Toast.makeText(getBaseContext(), Integer.toString(v.getId()), Toast.LENGTH_LONG).show();

     //int wieVieleReihen = tabellenLayout.getChildCount() - 1;
     try{
         int id1, id2, id3, tempPZ;
         id1 = viewEditText1.getId();
         String[] parts = Integer.toString(id1).split("90");
         parts[2] = Integer.toString(Integer.parseInt(parts[2]) + 2);
         id3 = Integer.parseInt(parts[0] + "90" + parts[1] + "90" + parts[2]);
         parts[2] = Integer.toString(Integer.parseInt(parts[2]) - 1);
         id2 = Integer.parseInt(parts[0] + "90" + parts[1] + "90" + parts[2]);

         //kontrolle.setText("ID1: " + id1 + " ID2: " + id2 + " ID3: " + id3);
         //viewEditText1.requestFocus();
         /*INSIDE HERE SHOULD NOT BE A MISTAKE    
        try {//es müssen unbedingt werte eingegeben worden sein
        if (false) {
             //((EditText) findViewById(id1)).getText().toString().equals("") || ((EditText) findViewById(id1)).getText().toString().trim().length() == 0 || (((EditText) findViewById(id2)).getText().toString().equals("")) || ((EditText) findViewById(id2)).getText().toString().trim().length() == 0
             //Toast.makeText(getBaseContext(), "Werte eingeben und bestaetigen!", Toast.LENGTH_SHORT).show();
         } else
               tempPZ = 0; //keine  Ahnung warum

              //((EditText) findViewById(id1)).;

             tempPZ = berechnePunktzahl(Integer.parseInt(((EditText) findViewById(id1)).getText().toString()), Integer.parseInt(((EditText) findViewById(id2)).getText().toString()));
                        ((TextView) findViewById(id3)).setText(Integer.toString(tempPZ));

             int c;
             int summe = 0;
             int tempoID = 0;
             for (c = 0; c < runde; c++) {

             tempoID = Integer.parseInt((c + 1) + "90" + parts[1] + "90" + "3");

            summe = summe + Integer.parseInt(((TextView) findViewById(tempoID)).getText().toString());
             }

             ((TextView) findViewById(Integer.parseInt("222" + parts[1]))).setText(Integer.toString(summe));

            //punktZahlen.add(Integer.parseInt(parts[0])-1,tempPZ);

            //Integer sum = 0;
            //for ( Integer i : punktZahlen ) {
            //    sum += i;
            //}

           //((TextView) findViewById(Integer.parseInt("222"+parts[1]))).setText(Integer.toString(sum));
          } catch (Exception e) {
               //Toast.makeText(getBaseContext(), "Werte eingeben!", Toast.LENGTH_LONG).show();
          }
                */ INSIDE HERE SHOULD NOT BE A MISTAKE

                }   catch(Exception e){ //this throws NullPointerException
                    Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

这是错误 Stacktrace:

07-17 12:41:05.250  32282-32282/com.martins.martin.bestellen E/YOUR_APP_LOG_TAG﹕ I got an error
java.lang.NullPointerException
        at com.martins.martin.bestellen.SecondActivity.onTextChanged(SecondActivity.java:278)
        at android.widget.TextView.sendOnTextChanged(TextView.java:8910)
        at android.widget.TextView.setText(TextView.java:4866)
        at android.widget.TextView.setText(TextView.java:4716)
        at android.widget.EditText.setText(EditText.java:109)
        at android.widget.TextView.setText(TextView.java:4691)
        at android.widget.TextView.onRestoreInstanceState(TextView.java:4583)
        at android.view.View.dispatchRestoreInstanceState(View.java:13722)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2849)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2849)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2849)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2849)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2849)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2849)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2849)
        at android.view.View.restoreHierarchyState(View.java:13700)
        at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1952)
        at android.app.Activity.onRestoreInstanceState(Activity.java:983)
        at android.app.Activity.performRestoreInstanceState(Activity.java:955)
        at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1144)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
        at android.app.ActivityThread.access00(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5476)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

希望您能找到问题所在。应该对键盘做些什么(任何缓冲区????)

非常感谢, 马丁

现在我通过编程模拟 DEL 键按下解决了这个问题。这是一个非常糟糕的解决方案,但我找不到问题...