I'm currently having a problem with my app whereby occassionally users are clicking on one of my options and being presented with a NullPointerException. The problem being, the error reports contain no information and I can't figure out why it happens. This is the stack trace submitted:
java.lang.NullPointerException
at com.espian.formulae.ListMenu.thermoItemClick(ListMenu.java:754)
at com.espian.formulae.ListMenu$2.onItemClick(ListMenu.java:386)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3672)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1829)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
and this is the offending method:
protected void thermoItemClick(int position) {
Intent i = new Intent();
TextView t = (TextView)lv.getChildAt(position);
switch (position) {
case 0: //Thermo functions
i.setClass(getApplicationContext(), Tab_Hoster.class);
i.putExtra(Tab_Hoster.EXTRA, Tab_Hoster.TAB_THERMO);
break;
case 1: //C-C equation
case 2: // C equation
case 3: //vant hoff
case 4:
case 5:
case 6:
i.setClass(getApplicationContext(), Equation.class);
break;
default:
i = null;
}
if (i != null) {
String s = (String) t.getText();
i.putExtra(States.KEY_E, s);
i.putExtra(States.TABLE, States.EQUATION);
startActivity(i);
}
if (cleanscreen)
this.finish();
}
If it helps, it seems to this normally seems to occur only the first time people select this option and tends to be fine after that. I've had no occurrences of this exception myself, and I've only had 14 reports of it but it's annoying and I'd like to get to the bot开发者_如何学Ctom of it.
Thanks
If TextView t = (TextView)lv.getChildAt(position);
is incorrectly called (i.e., lv doesn't include any View in this position) then t will be null and t.getText()
will throw an exception.
In General I recommend to avoid loading Views using ViewGroup.getChildAt(). it's just not safe. I'm guessing you dynamically build a layout, that's why you can't findViewById this TextView. What I do in cases like these, when I dynamically build layouts, I also allocate IDs to these layouts (store them in a map or something in memory) and then using the very-safe findViewById method.
my guess is that your position variable is not matching any of those cases. Also try different versions of the emulator, could be version specific.
Since it only happens the first time then something is most likely not initialized. Where does the variable cleanscreen
come from? Is it a global variable? Is it initialized during onCreate, or when it is declared?
Most likely the textview is empty. If t is empty (null) then
String s = (String) t.getText()
will be null.
精彩评论