【发布时间】:2016-11-30 07:35:23
【问题描述】:
我得到以下堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference --------- Stack trace --------- mypackage.Inbox$9.onclick(Inbox.java:1538) android.view.View.performClick(View.java:5204) android.view.View$PerformClick.run(View.java:21153) android.os.Handler.handleCallback(Handler.java:739) android.os.Handler.dispatchMessage(Handler.java:95) android.os.Looper.loop(Looper.java:148) android.app.ActivityThread.main(ActivityThread.java:5444) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
double taxvalue = 0.0;
double acttotal=Global.getDouble(((EditText)container.findViewById(R.id.textView9)).getText().toString());
taxvalue = Global.getDouble(((EditText) container.findViewById(R.id.textView8)).getText().toString());
((EditText) (table.getChildAt(1).findViewById(R.id.amount))).setText(Global.formatDouble(acttotal - taxvalue));
这里 table - LinearLayout,最后一行是第 1538 行。
我试过 table=null; 但无法复制。有什么机会得到这个问题。
【问题讨论】:
-
'void android.widget.EditText.setText(java.lang.CharSequence)' 为空
-
setText 也将接受数字作为输入,然后在 R.id 中搜索。在 setText 方法中使用之前,您应该尝试将数字转换为字符串。
-
嗨,sushildlh,感谢您的回复。但我不是在问什么是空指针异常..从我的代码中有时会得到空指针异常,但我无法重现这些。有时我的用户会得到这个,这就是我问的原因
-
这看起来有问题:
table.getChildAt(1).findViewById(R.id.amount)。您与getChildAt得到的孩子可能是EditText。删除getChildAt(1)或删除findViewById -
嗨 Orkun,你认为它会因为 String - numbers 变化而引发 nullpointer 异常吗?
标签: android android-layout nullpointerexception