Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
-java.lang.NullPointerException-空对象引用上的setText_Java_Android_Nullpointerexception - Fatal编程技术网

-java.lang.NullPointerException-空对象引用上的setText

-java.lang.NullPointerException-空对象引用上的setText,java,android,nullpointerexception,Java,Android,Nullpointerexception,这就是我几个小时来一直在努力做的事情: 我有一个MainActivity.java文件(如下所示)和一个带有开始按钮的fragment_start.xml文件。点击开始按钮应显示带有points-/round-和countdown文本视图的activity_main.xml文件。它不起作用,这就是正在发生的事情: logcat告诉我: PID:1240 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextV

这就是我几个小时来一直在努力做的事情: 我有一个MainActivity.java文件(如下所示)和一个带有开始按钮的fragment_start.xml文件。点击开始按钮应显示带有points-/round-和countdown文本视图的activity_main.xml文件。它不起作用,这就是正在发生的事情:

logcat告诉我: PID:1240 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)

模拟器显示:很遗憾,游戏已经停止。

有必要提到我是编程新手吗

谢谢你的建议

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();
}
}

这就是你的问题所在:

private void fillTextView (int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text); // tv is null
}
-->(文本视图)findViewById(id);//返回空值 但是从您的代码中,我找不到这个方法返回null的原因。设法追查, 作为参数提供的id,以及此具有指定id的视图是否存在

错误信息非常清楚,甚至可以告诉您使用什么方法。 从文件中:

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


换句话说:您没有以给定id作为参数的视图。

问题在于
tv.setText(text)
。变量tv可能是
null
,您可以在该
null
上调用
setText
方法,但您不能这样做。 我猜问题出在
findviewbyd
方法上,但它不在这里,所以如果没有代码,我就说不出更多

private void fillTextView (int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text);
}
如果这是获取空指针异常的地方,则找不到传递到
findViewById()
的id的视图,并且在
null
上尝试调用函数
setText()
时引发实际异常。您应该为
R.layout.activity\u main
发布XML,因为仅仅通过查看代码很难判断哪里出了问题


更多关于空指针的阅读:

发布整个异常消息/logcat会有所帮助。请发布您的xml文件。请看我的答案。这个解释再清楚不过了,但你必须找到自己在查找TextView
TextView tv=(TextView)findViewById(id)时为什么将错误id作为参数(因为如果存在具有此id的视图,该方法将不会返回null)使用findViewById,您必须确定正在查看的布局。如果您正在查看充气布局内部,如下所示
查看视图=充气器.inflate(R.layout.inflatedLayout,null)
必须指定要查看的视图,如so
TextView tv=(TextView)theView.findViewById(id)请参阅以下链接。是的,这就是问题所在。此处的错误消息非常清楚。您可以尝试检查是否已使用相同的ID声明了两个或多个textview