Issue
This an example from Big Nerd Ranch Guide:
package com.example.geoquiz;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_africa, true),
new TrueFalse(R.string.question_americas, false),
new TrueFalse(R.string.question_asia, false),
new TrueFalse(R.string.question_mideast, true),
new TrueFalse(R.string.question_oceans, true)
};
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.true_button);
mFalseButton = (Button)findViewById(R.id.false_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The part in question is this:
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
question
is an int
here, I wonder how this works. More strangely, if I change question
into a literal int, e.g. 1, then the app won't work.
Solution
The Docs I thought described this better so I'm not sure if they changed them recently (or since I last looked).
Anyway, when you supply an int
as the param
it refers to a resource id
. So, if you have a string resource
in your strings.xml
you can supply it here instead of a literal String
.
So, if in your strings.xml
you have
<resources>
<string name="hello">Hello!</string>
</resources>
you could do
myTV.setText(R.string.hello);
and myTV
would show "Hello".
When you pass it an int
that doesn't match an id
in R.strings
then you get the exception.
Answered By - codeMagic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.