【问题标题】:- java.lang.NullPointerException - setText on null object reference [duplicate]- java.lang.NullPointerException - 空对象引用上的 setText [重复]
【发布时间】:2015-03-10 18:07:24
【问题描述】:

这就是我试图做几个小时的事情: 我有一个 MainActivity.java 文件(下面列出)和一个带有开始按钮的 fragment_start.xml 文件。点击开始按钮应显示带有点/圆形和倒计时文本视图的 activity_main.xml 文件。它不起作用,这就是正在发生的事情:

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

模拟器显示:很遗憾,GAME 已停止。

有必要说我是编程新手吗?

感谢您的建议!

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

【问题讨论】:

  • 发布整个异常消息/logcat 会有所帮助。
  • 请发布您的 xml 文件。
  • 看我的回答。解释再清楚不过了,但你必须自己追查为什么你给一个错误的 id 作为参数(因为如果存在具有此 ID 的视图,该方法将不会返回 null)
  • 当您使用 findViewById 查找 TextView TextView tv = (TextView) findViewById(id); 时,您必须确定您正在查看的布局。如果您正在查看像 View theView = inflater.inflate(R.layout.inflatedLayout, null); 这样的膨胀布局,您必须指定您正在查看的视图,如 TextView tv = (TextView) theView.findViewById(id);
  • 参考以下链接。 vshivam.wordpress.com/2015/01/14/…

标签: java android nullpointerexception


【解决方案1】:

问题是tv.setText(text)。变量 tv 可能是 null 并且您在该 null 上调用 setText 方法,但您不能这样做。 我的猜测是问题出在findViewById 方法上,但它不在这里,所以我不能告诉更多,没有代码。

【讨论】:

  • 是的,这就是问题所在。这里的错误信息很清楚。
  • 您可以尝试检查您是否声明了两个或多个具有相同ID的textview
【解决方案2】:

这是你的问题:

private void fillTextView (int id, String text) {
    TextView tv = (TextView) findViewById(id);
    tv.setText(text); // tv is null
}

--> (TextView) 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

http://developer.android.com/reference/android/view/View.html#findViewById%28int%29

换句话说:您没有将 id 作为参数提供的视图。

【讨论】:

    【解决方案3】:
    private void fillTextView (int id, String text) {
        TextView tv = (TextView) findViewById(id);
        tv.setText(text);
    }
    

    如果这是您获得空指针异常的地方,则没有找到您传递给findViewById() 的 id 的视图,当您尝试在 @ 上调用函数 setText() 时会引发实际异常987654325@。您应该为R.layout.activity_main 发布您的 XML,因为仅通过查看您的代码很难判断哪里出了问题。

    更多关于空指针的阅读:What is a NullPointerException, and how do I fix it?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 2021-05-04
      相关资源
      最近更新 更多