Hello Android开发者,
当EditText更改其文本时,我在onTextChanged()方法中不断收到NullPointerException。所有智能手机上都没有但。 LG G2不会出现任何错误,例如三星Galaxy S 4。我有两个EditTexts。通过输入数字,程序获取两个值并计算一些东西。真奇怪的是,当键盘打开并按任意键然后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$2.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.access$1000(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)
我希望你能找到问题所在。应该用键盘做任何事情(任何Buffer ????)
非常感谢, 马丁
答案 0 :(得分:0)
现在我通过编程方式对DEL-Keypress进行了解决。这是一个非常糟糕的解决方案,但我找不到问题......