Hello Android developer,
i keep getting a NullPointerException in my onTextChanged() method when EditText changes its text. BUT not on all smartphones. LG G2 does not throw any errors, but eg Samsung Galaxy S 4 does. I have two EditTexts. By entering numbers the program takes both values and calculates something. And really strange is that when the keyboard opens and i press any key and then DEL-key everything works.
here some code:
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) {
}
});
This is the error 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)
I hope you will find the problem. Should have to do something with the Keyboard (any Buffer????)
Thanks a lot, Martin
Now I solved it by simulting a DEL-Keypress programmatically. Its a very bad solution but i could not find the problem...