- java.lang.NullPointerException - 空对象引用上的 setText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27911066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
- java.lang.NullPointerException - setText on null object reference
提问by George
This is what I'm trying to do for several hours: I've got a MainActivity.java file (listing below) and a fragment_start.xml file with a start button. Tapping the start-button should display the activity_main.xml file with points-/round- and countdown-Textviews. It doesn't work and this is what is happening:
这就是我几个小时以来一直在尝试做的事情:我有一个 MainActivity.java 文件(如下所列)和一个带有开始按钮的 fragment_start.xml 文件。点击开始按钮应该会显示带有点/回合和倒计时文本视图的 activity_main.xml 文件。它不起作用,这就是正在发生的事情:
The logcat tells me: PID: 1240 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
logcat 告诉我: PID: 1240 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
The emulator displays: Unfortunately, GAME has stopped.
模拟器显示:不幸的是,GAME 已停止。
Necessary to mention that I'm rather new in programming?
有必要提到我在编程方面还很陌生吗?
Thanks for any advice!
感谢您的任何建议!
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private int points;
private int round;
private int countdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showStartFragment();
}
private void newGame () {
points=0;
round=1;
initRound();
}
private void initRound() {
countdown = 10;
update();
}
private void update () {
fillTextView(R.id.points, Integer.toString(points));
fillTextView(R.id.round, Integer.toString(round));
fillTextView(R.id.countdown, Integer.toString(countdown * 1000));
}
private void fillTextView (int id, String text) {
TextView tv = (TextView) findViewById(id);
tv.setText(text);
}
private void showStartFragment() {
ViewGroup container = (ViewGroup) findViewById(R.id.container);
container.removeAllViews();
container.addView(
getLayoutInflater().inflate(R.layout.fragment_start, null) );
container.findViewById(R.id.start).setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.start) {
startGame();
}
}
public void startGame() {
newGame();
}
}
回答by ProgrammingIsAwsome
Here lies your problem:
你的问题就在这里:
private void fillTextView (int id, String text) {
TextView tv = (TextView) findViewById(id);
tv.setText(text); // tv is null
}
--> (TextView) findViewById(id); // returns null But from your code, I can't find why this method returns null. Try to track down, what id you give as a parameter and if this view with the specified id exists.
--> (TextView) findViewById(id); // 返回 null 但是从您的代码中,我找不到此方法返回 null 的原因。尝试追踪您提供的 id 作为参数,以及具有指定 id 的视图是否存在。
The error message is very clear and even tells you at what method. From the documentation:
错误信息很清楚,甚至会告诉你用什么方法。从文档:
public final View findViewById (int id)
Look for a child view with the given id. If this view has the given id, return this view.
Parameters
id The id to search for.
Returns
The view that has the given id in the hierarchy or null
http://developer.android.com/reference/android/view/View.html#findViewById%28int%29
http://developer.android.com/reference/android/view/View.html#findViewById%28int%29
In other words: You have no view with the id you give as a parameter.
换句话说:您没有将 id 作为参数提供的视图。
回答by JustOneJavaDev
The problem is the tv.setText(text)
. The variable tv is probably null
and you call the setText
method on that null
, which you can't.
My guess that the problem is on the findViewById
method, but it's not here, so I can't tell more, without the code.
问题是tv.setText(text)
. 变量 tv 可能是null
,您可以调用该setText
方法null
,而您不能。我猜测问题出在findViewById
方法上,但不在这里,所以如果没有代码,我不能说更多。
回答by mgomov
private void fillTextView (int id, String text) {
TextView tv = (TextView) findViewById(id);
tv.setText(text);
}
If this is where you're getting the null pointer exception, there was no view found for the id that you passed into findViewById()
, and the actual exception is thrown when you try to call a function setText()
on null
. You should post your XML for R.layout.activity_main
, as it's hard to tell where things went wrong just by looking at your code.
如果这就是你得到的空指针异常,没有发现该ID,你传递给视图findViewById()
,而当你试图调用一个函数实际抛出异常setText()
上null
。您应该为 发布您的 XML R.layout.activity_main
,因为仅通过查看您的代码很难判断哪里出了问题。
More reading on null pointers: What is a NullPointerException, and how do I fix it?
关于空指针的更多阅读:什么是 NullPointerException,以及如何修复它?