NullPointerException on TextView
- by Stephen Adipradhana
i get a null pointer exception and the program crash on each time i want to update the highscore text using setText(). what causes this problem?
this code is when i set my layout, the layout is a part of the gameView using opengl, and i put the highscore textview on the upper left corner
public void onCreate(Bundle savedInstanceState) {
SFEngine.display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();//ambl ukuran width height layar
super.onCreate(savedInstanceState);
gameView = new SFGameView(this);
gameView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView textBox = new TextView(this);
textBox.setId(1);
textBox.setText("HIGH SCORE");
textBox.setBackgroundColor(Color.BLUE);
textBox.setWidth(SFEngine.display.getWidth()/2);
textBox.setHeight(50);
Button pauseButton = new Button(this);
pauseButton.setText("PAUSE");
pauseButton.setHeight(50);
pauseButton.setWidth(SFEngine.display.getWidth()/2);
pauseButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
//pause game
SFEngine.isPlaying = false;
Intent i1 = new Intent(SFGames.this, pause.class);
gameView.onPause();
startActivityForResult(i1,0);//hrs pk result soalny mw blk lg
return true;
}
});
RelativeLayout.LayoutParams lp_pause = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp_hs = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp_hs.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp_pause.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp_pause.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textBox.setLayoutParams(lp_hs);
pauseButton.setLayoutParams(lp_pause);
layout.addView(gameView);
layout.addView(textBox);
layout.addView(pauseButton);
setContentView(layout);
and here is the setText code
public boolean onTouchEvent (MotionEvent event){//buat nerima input user
if(!SFEngine.isPlaying){
finish();
}
textBox.setText("High Score :" + SFEngine.score);//here is the source of the prob
.....