dimanche 15 mars 2015

Hide checkboxes in different situations of other backgrounds

I am creating this activity that a question is fetched from string-array using random. In different questions, I am giving a different background to my activity to match the question's topic. What I have is an on click listener for check boxes, where the user is needed to press on the correct check box for the selected question. I want to show different check boxes in different backgrounds (& questions) so I have implemented all the checkboxes in my layout including the android:visibility="gone" and trying to checkBox1.setVisibility(View.VISIBLE); I keep getting an NullPointerException in the line where I set the visibility of the checkbox to visible. Can somebody explain me that? code is below



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tennis_facts);

TextView countsTxt = (TextView) findViewById(R.id.txtCounts);
questions = getResources().getStringArray(R.array.questions);
CheckBox centerBack = (CheckBox) findViewById(R.id.centerBack);
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
CheckBox checkBox6 = (CheckBox) findViewById(R.id.checkBox6);

centerBack.setOnClickListener(this);
checkBox1.setOnClickListener(this);
checkBox2.setOnClickListener(this);
checkBox3.setOnClickListener(this);
checkBox4.setOnClickListener(this);
checkBox5.setOnClickListener(this);
checkBox6.setOnClickListener(this);

countsTxt.setTextColor(Color.parseColor("#0033CC"));
countsTxt.setTextSize(24);
countsTxt.setText("Tries: " + triesLeft);

}

@Override
protected void onResume() {
super.onResume();

counts = 0;
getQuestion();
}

@SuppressLint("NewApi") private void getQuestion(){
TextView questionText = (TextView) findViewById(R.id.question);
questionText.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
questionText.setTextSize(24);

RelativeLayout screenGame = (RelativeLayout) findViewById(R.id.screenGame);

Random rand = new Random();
int maxIndex = questions.length;
int generatedIndex = rand.nextInt(maxIndex);
if(questions[generatedIndex].equals("Click on the player with the possition of a Center Back")){
screenGame.setBackground(getResources().getDrawable(R.drawable.footballfindtheposition));
checkBox1.setVisibility(View.VISIBLE);
}
else if(questions[generatedIndex].equals("Click on the area where Player should serve in order to be valid")){
screenGame.setBackground(getResources().getDrawable(R.drawable.tenniscourt));
}
questionText.setText(questions[generatedIndex]);
}




Aucun commentaire:

Enregistrer un commentaire