TextView txtOtherMatches = (TextView) dialog.findViewById(R.id.txtOtherMatches);
txtOtherMatches.setText("Other Matches");
即时得到这个错误,同时运行的应用程序和IM只是在运行时分配简单的文字TextView的..... 显示java.lang.NullPointerExceptionJava运行时异常
TextView txtOtherMatches = (TextView) dialog.findViewById(R.id.txtOtherMatches);
txtOtherMatches.setText("Other Matches");
即时得到这个错误,同时运行的应用程序和IM只是在运行时分配简单的文字TextView的..... 显示java.lang.NullPointerExceptionJava运行时异常
android:id="@+id/txtOtherMatches"
res/layout
文件夹(例如,layout-normal
,layout-large
或layout-land
),如果你这样做,确保你的TextView存在于对应于你的设备/模拟器的layourtViewGroup
)txtOtherMatches
可能是因为null
它doesn” t在当前视图中存在。当您使用findViewById
时,视图必须位于您使用setContentView
设置的“contentview”中,或者稍后添加到视图中。您无法找到任何具有某个ID的“随机”视图,但它实际上必须在当前视图中“呈现”。
如果视图是不是在你使用已经是XML,但别的地方在你个XML的一个,你必须使用inflater
获得查看,并与View.add()
,如果你提供一些更多的行这将是有益的在那之前。 但我的猜测是南恩是对的。
尝试添加安全检查: (这样不会改变代码流)
if(txtOtherMatches == null) {
throw new NullPointerException("darn, the R.id.txtOtherMatches is not in the dialog")
}
(在另一方面Nanne错误地提到“当前视图”,而我敢肯定,他指的是“对话“视图 如果您需要进一步的帮助,请多放些肉;)
是的,清洁问题解决后...非常感谢:) – nileshbirhade 2011-05-09 09:13:01
@niles很高兴工作。那么请考虑接受答案。请参阅http://meta.stackexchange.com/questions/16721/how-does-accept-rate-work – Aleadam 2011-05-09 14:47:37