BigNerdRanch android教程,Geoquick中的类强制转换异常

BigNerdRanch android教程,Geoquick中的类强制转换异常,android,Android,真的找不到窃听器在哪里。当按下“作弊”按钮(启动第二个活动)时,应用程序就会崩溃。 抛出类强制转换异常 GeoQuizActivity.java: package com.bignerdranch.android.quick; 导入android.app.Activity; 导入android.content.Intent; 导入android.os.Bundle; 导入android.util.Log; 导入android.view.view; 导入android.widget.Button

真的找不到窃听器在哪里。当按下“作弊”按钮(启动第二个活动)时,应用程序就会崩溃。 抛出类强制转换异常

GeoQuizActivity.java:

package com.bignerdranch.android.quick;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageButton;
导入android.widget.TextView;
导入android.widget.Toast;
公共类GeoquizaActivity扩展了活动{
/**在首次创建活动时调用*/
私有静态最终字符串TAG=“GeoQuizActivity”;
私有静态最终字符串键\u INDEX=“INDEX”;
私人按钮;
专用按钮mFalseButton;
专用按钮mCheatButton;
私有图像按钮mnext按钮;
私有图像按钮MPREV按钮;
私有文本视图mQuestionTextView;
私有布尔混合型;
私有void updateQuestion(){
int question=mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(问题);
}
专用void checkAnswer(布尔用户pressedtrue){
布尔值answerIsTrue=mQuestionBank[mCurrentIndex].isTrueQuestion();
int messageResId=0;
如果(恶作剧者){
messageResId=R.string.decision\u toast;
}
否则{
if(userPressedTrue==answerIsTrue){
messageResId=R.string.correct\u toast;
}否则{
messageResId=R.string.error\u toast;
}
Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
如果(数据==null){
返回;
}
mIsCheater=data.getBooleanExtra(显示的是CheatActivity.EXTRA\u ANSWER\u,false);
}
私有TrueFalse[]mQuestionBank=新TrueFalse[]{
新的TrueFalse(R.string.question_,true),
新的TrueFalse(R.string.question_Middeast,false),
新的TrueFalse(R.string.question_africa,false),
新的TrueFalse(R.string.question_americas,true),
新TrueFalse(R.string.question_asia,true),
};
private int mCurrentIndex=0;
@凌驾
创建时的公共void(Bundle savedInstanceState){
d(标记“OnCreate(Bundle)called”);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
如果(savedInstanceState!=null){
mCurrentIndex=savedInstanceState.getInt(键索引,0);
}
mQuestionTextView=(TextView)findViewById(R.id.question\u text\u视图);
mQuestionTextView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
mCurrentIndex=(mCurrentIndex+1)%mQuestionBank.length;
updateQuestion();
}
});
mTrueButton=(按钮)findViewById(R.id.true\u按钮);
mTrueButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
核对答案(正确);
}
});
mFalseButton=(按钮)findViewById(R.id.false_按钮);
mFalseButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
核对答案(假);
}
});
mNextButton=(ImageButton)findViewById(R.id.next_按钮);
mNextButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
mCurrentIndex=(mCurrentIndex+1)%mQuestionBank.length;
updateQuestion();
淘气者=假;
}
});
mPrevButton=(ImageButton)findViewById(R.id.prev_按钮);
mPrevButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(mCurrentIndex==0){
mCurrentIndex=mQuestionBank.length-1;
}
否则{
mcurrentidex=(mcurrentidex-1);
}
updateQuestion();
淘气者=假;
}
});
mCheatButton=(按钮)findviewbyd(R.id.cheat_按钮);
mCheatButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向i=新意向(GeoQuizActivity.this、CheatActivity.class);
布尔值answerIsTrue=mQuestionBank[mCurrentIndex].isTrueQuestion();
i、 putExtra(作弊活动。额外答案为真,回答为真);
startActivityForResult(i,0);
}
});
updateQuestion();
}
@凌驾
SaveInstanceState上的公共无效(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.i(标记“onSaveInstanceState”);
savedInstanceState.putInt(键索引,mCurrentIndex);
}
@凌驾
public void onStart(){
super.onStart();
d(标记“onStart()调用”);
}
@凌驾
公共无效暂停(){
super.onPause();
d(标记“onPause()调用”);
}
@凌驾
恢复时公开作废(){
super.onResume();
d(标记“onResume()已调用”);
}
@凌驾
公共void onStop(){
super.onStop();
d(标记“onStop()已调用”);
}
@凌驾
公共空间{
super.ondestory();
d(标记“onDestroy()已调用”);
}

}
Logcat是你的朋友:

...
at com.bignerdranch.android.quiz.CheatActivity.onCreate(CheatActivity.java:28) 
...

显然,您正试图将对象强制转换到某个不可从中赋值的类。检查CheatActivity.java,第28行。

请发布日志,并指定引发异常的行!找到了,谢谢!第28行mShowAnswer=(按钮)findViewById(R.id.showAnswerButton);引导到activity_cheat.xml,在那里我再次输入而不是感谢!