简体   繁体   English

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

[英]Why does this class, when the method onResume is called, give error NullPointerException?

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

} }

The layout should be loaded first. 应首先加载布局。 You forget to call setContentView() . 你忘了调用setContentView()

That's why these controls 这就是为什么这些控制

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

return null 返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM