为什么这个类在调用onResume方法时会给出错误NullPointerException?

时间:2013-05-20 02:41:53

标签: android nullpointerexception onresume

public class Scores extends Activity {

TextView tvPoints;
TextView tvRecord;

int record = 0;
int points = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    tvPoints = (TextView) findViewById(R.id.tvPoints);
    tvRecord = (TextView) findViewById(R.id.tvRecord);

    points = getIntent().getIntExtra("points", 0);
    record = getIntent().getIntExtra("record", 0);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    tvPoints.setText("Points: "+points);
    tvRecord.setText("Record: "+record);
}

}

1 个答案:

答案 0 :(得分:2)

应首先加载布局。你忘了打电话给setContentView()

这就是为什么这些控件

tvPoints = (TextView) findViewById(R.id.tvPoints);
tvRecord = (TextView) findViewById(R.id.tvRecord);

返回null