[英]setting int drawable resource to onClick
我目前正在研究10個問題的測驗應用程序,因此我需要在選定問題字段中顯示5張圖像,並在單擊文本時顯示提示對話框作為提示。 我設置了可繪制資源文件,以將每個單獨的圖像顯示為引用的文本。 單擊時如何正確將資源可繪制int設置為文本? 目前,出現錯誤。
QuestionLibrary.java
// This file contains questions from QuestionBank
class QuestionLibrary {
// array of questions
private String mQuestions [] = {
"Question1",
"Question2",
"Question3",
"Question4?",
"Qustion5",
"Question6",
"Question7",
"Question8",
"Question9",
"Question10"
};
// array of multiple choices for each question
private String mChoices [][] = {
// choices goes here
};
//setting images
public static int[] image_drawables = new int[] {
R.drawable_01, R.drawable_02, R.drawable_03, R.drawable_04
};
// array of correct answers - in the same order as array of questions
private String mCorrectAnswers[] = {
Answers goes here
};
// text to show hint reference
private static String[] click_me_text = {
"", "", "Hint", "", "Hint", "", "", "Click me!", "Click me!", "", "",
};
// method returns click_me text from array textQuestions[] based on array index
String getTextHint(int a) {
return click_me_text[a];
}
// method returns number of questions
int getLength(){
return mQuestions.length;
}
// method returns question from array textQuestions[] based on array index
String getQuestion(int a) {
return mQuestions[a];
}
// method return a single multiple choice item for question based on array index,
// based on number of multiple choice item in the list - 1, 2, 3 or 4 as an argument
String getChoice(int index, int num) {
return mChoices[index][num-1];
}
// method returns correct answer for the question based on array index
String getCorrectAnswer(int a) {
return mCorrectAnswers[a];
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private QuestionLibrary mQuestionLibrary = new QuestionLibrary();
//initializing buttons
Button button1;
Button button2;
Button button3;
Button button4;
TextView msingleQuestion;
TextView mQuestion; //current question to answer
TextView mClickMe; // text to be clicked which should display image
private int mQuestionNumber = 0; // current question number
private String mAnswer; // correct answer for question in mQuestionView
private int mquizNumber = 1;
private int mfailedQuestions = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_begineer);
// set textViews here
msingleQuestion = (TextView) findViewById(R.id.singleQuestion);
mQuestion = (TextView) findViewById(R.id.txtQuestion);
mClickMe = (TextView) findViewById(R.id.click_me);
button1 = (Button) findViewById(R.id.firstOption);
button2 = (Button) findViewById(R.id.secondOption);
button3 = (Button) findViewById(R.id.thirdOption);
button4 = (Button) findViewById(R.id.fourthOption);
updateQuestion(); //update question
// setting respective animations for the buttons
blinkClickMe();
}
private void blinkClickMe() {
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(60);
anim.setStartOffset(30);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
mClickMe.startAnimation(anim);
}
private void updateQuizNumber(int mquizNumber) {
msingleQuestion.setText("" + mquizNumber+"/"+mQuestionLibrary.getLength());
}
private void updateQuestion() {
// check if we are not outside array bounds for questions
if(mQuestionNumber<mQuestionLibrary.getLength() ){
// set text for hint and click_me
mClickMe.setText(mQuestionLibrary.getTextHint(mQuestionNumber));
// set the text for new question, and new 4 alternative to answer on four buttons
mQuestion.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
button1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
button2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
button3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
button4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
mQuestionNumber++;
}
else {
Toast.makeText(BeginnerActivity.this, "It was the last question!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MenuOptions.class);
// intent.putExtra("score", mScore); // pass the current score to the second screen
startActivity(intent);
}
}
public void onClick(View view) {
//all logic for all answers buttons in one method
final Button answer = (Button) view;
// if the answer is correct, increase the score
if (answer.getText().equals(mAnswer) ){
answer.setBackgroundColor(Color.GREEN);
// blink correct answer
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(60);
anim.setStartOffset(30);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.RELATIVE_TO_PARENT);
answer.startAnimation(anim);
}else {
answer.setBackgroundColor(Color.RED);
mfailedQuestions--;
updateLives(mfailedQuestions);
}
if (mQuestionNumber < mQuestionLibrary.getLength()) {
// once user answer the question, we move on to the next one, if any
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
answer.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.rounded_button_questions));
}
updateQuestion();
mquizNumber++;
updateQuizNumber(mquizNumber);
}
}, 1000);
} else {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(BeginnerActivity.this, MenuOptions.class);
// intent.putExtra("score", mScore); // pass the current score to the second screen
startActivity(intent);
}
}, 1000);
}
}
public void revealImage(View view) {
loadImage();
}
private void loadImage() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
// what is needed here?
View dialogView = inflater.inflate(QuestionLibrary.image_drawables[mQuestionNumber], null);
builder.setView(dialogView)
.create().show();
}
@Override
public void onFinish() {
timer.setText("time's up!!");
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(60);
anim.setStartOffset(30);
anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(Animation.RELATIVE_TO_PARENT);
timer.startAnimation(anim);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 500);
}
}
}
錯誤致命異常:主要
進程:com.example.benkeys.pianoquizgame,PID:3961 java.lang.IllegalStateException:無法在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)上執行android:onClick的方法android.os.Handler.handleCallback(Handler.java:751)上的android.view.View $ PerformClick.run(View.java:22429)上的.view.View.performClick(View.java:5637)。 java.lang.reflect.Method.invoke上的Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(ActivityThread.java:6119) (原生方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)造成原因:java.lang android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)上的java.lang.reflect.Method.invoke(本機方法)處的.reflect.InvocationTargetException android.os.Handler.dispatchMessage(Handler)上的rformClick(View.java:5637)android.view.View $ PerformClick.run(View.java:22429)android.os.Handler.handleCallback(Handler.java:751) .java:95),位於android.os.Looper.loop(Looper.java:154),位於android.app.ActivityThread.main(ActivityThread.java:6119),位於java.lang.reflect.Method.invoke(本機方法),位於com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)位於com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)原因:android.content.res.Resources $ NotFoundException:來自xml類型布局資源ID#0x7f060056的xml / layable-xxhdpi-v4 / beginner_04.png位於android.content.res.ResourcesImpl.loadXmlResourceParser(ResourcesImpl.java:990)位於android.content.res.Resources.loadXmlResourceParser( android.content.res.Resources.getLayout(Resources.java:1115)上的Resources.java:2103)android.view.LayoutInflater.inflate(LayoutInflater.java)上android.view.LayoutInflater.inflate(LayoutInflater.java:424)上的Resources.java:2103) :377 )的com.example.benkeys.pianoquizgame.BeginnerActivity.loadImage(BeginnerActivity.java:244)的com.example.benkeys.pianoquizgame.BeginnerActivity.revealImage(BeginnerActivity.java:237)的java.lang.reflect.Method.invoke( android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)上的android.view.View.performClick(View.java:5637)上的android.view.View $ PerformClick.run( View.java:22429),位於android.os.Handler.handleCallback(Handler.java:751),位於android.os.Handler.dispatchMessage(Handler.java:95),位於android.os.Looper.loop(Looper.java:154) )的android.app.ActivityThread.main(ActivityThread.java:6119)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886的java.lang.reflect.Method.invoke(本機方法) )在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)造成原因:java.io.FileNotFoundException:android.content.res.AssetManager.openXmlAssetNative(Nati ve方法)
您可以使用這種方法:
getResources().getDrawable(R.drawable.drawable)
在res / Drawable中設置您的可繪制對象,並通過上面的代碼獲取它,並將其放在對話框中。
如果您有多個可繪制對象,請使用switch()方法,如下所示:
switch(answeredQuestion)
{
case 1:
//show considered dialog box
break;
case 2:
// show considered dialog box
break;
...
case 10:
//show considered dialog box
default
break;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.