【问题标题】:What could be the reason for the following Nullpointer Exception [duplicate]以下 Nullpointer 异常的原因可能是什么 [重复]
【发布时间】: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


【解决方案1】:

有两个可能的原因:

1.您的数据可能是您在编辑文本中设置的null,请调试您的数据

2.首先初始化您在 OnCreate 中的 Edit Text 字段,例如

EditText edittext=(EditText).findViewById(R.id.Yourid);

【讨论】:

    猜你喜欢
    • 2011-01-20
    • 2017-01-05
    • 2012-02-26
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多