真的无法找出bug的位置。按下"作弊时,应用程序崩溃了#34;按钮(开始第二个活动)。 抛出转发异常
GeoQuizActivity.java:
package com.bignerdranch.android.quiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class GeoQuizActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "GeoQuizActivity";
private static final String KEY_INDEX ="index";
private Button mTrueButton;
private Button mFalseButton;
private Button mCheatButton;
private ImageButton mNextButton;
private ImageButton mPrevButton;
private TextView mQuestionTextView;
private boolean mIsCheater;
private void updateQuestion(){
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
int messageResId = 0;
if(mIsCheater){
messageResId=R.string.judgment_toast;
}
else{
if (userPressedTrue==answerIsTrue){
messageResId =R.string.correct_toast;
} else{
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(data==null){
return;
}
mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
}
private TrueFalse [] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_oceans, true),
new TrueFalse(R.string.question_mideast, false),
new TrueFalse(R.string.question_africa, false),
new TrueFalse(R.string.question_americas, true),
new TrueFalse(R.string.question_asia, true),
};
private int mCurrentIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "OnCreate(Bundle) called");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(savedInstanceState != null){
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
}
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
mCurrentIndex=(mCurrentIndex+1)%mQuestionBank.length;
updateQuestion();
}
});
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
checkAnswer(true);
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
checkAnswer(false);
}
});
mNextButton =(ImageButton)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
mCurrentIndex= (mCurrentIndex+1)%mQuestionBank.length;
updateQuestion();
mIsCheater=false;
}
});
mPrevButton = (ImageButton)findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(mCurrentIndex==0){
mCurrentIndex=mQuestionBank.length-1;
}
else{
mCurrentIndex=(mCurrentIndex-1);
}
updateQuestion();
mIsCheater=false;
}
});
mCheatButton = (Button)findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(GeoQuizActivity.this, CheatActivity.class);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
startActivityForResult(i, 0);
}
});
updateQuestion();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
}
@Override
public void onStart(){
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG, "onPause() called");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
public void onStop(){
super.onStop();
Log.d(TAG, "onStop() called");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
}

cheatActivity.java:
package com.bignerdranch.android.quiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CheatActivity extends Activity {
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.quiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.quiz.answer_shown";
private void setAnswerShownResult(boolean isAnswerShown){
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, true);
mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
setAnswerShownResult(false);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
}
else{
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
}
});
}
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:id="@+id/question_text_view" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" />
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/prev_button"
android:src="@drawable/arrow_left"
/>
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/next_button"
android:src="@drawable/arrow_right"
/>
<Button
android:id="@+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheat_button"/>
</LinearLayout>
</LinearLayout>
&#13;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/warning_text"/>
<TextView
android:id="@+id/answerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>
<TextView
android:id="@+id/showAnswerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_answer_button"/>
</LinearLayout>
&#13;
谢谢!
编辑:logCat:
我从顶部第3行开始t know which line causes the exception or how to find it. In logCat, it
s。
05-15 15:32:57.116: W/dalvikvm(349): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-15 15:32:57.256: E/AndroidRuntime(349): FATAL EXCEPTION: main
05-15 15:32:57.256: E/AndroidRuntime(349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.quiz/com.bignerdranch.android.quiz.CheatActivity}: java.lang.ClassCastException: android.widget.TextView
05-15 15:32:57.256: E/AndroidRuntime(349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.os.Looper.loop(Looper.java:123)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-15 15:32:57.256: E/AndroidRuntime(349): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 15:32:57.256: E/AndroidRuntime(349): at java.lang.reflect.Method.invoke(Method.java:507)
05-15 15:32:57.256: E/AndroidRuntime(349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 15:32:57.256: E/AndroidRuntime(349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 15:32:57.256: E/AndroidRuntime(349): at dalvik.system.NativeStart.main(Native Method)
05-15 15:32:57.256: E/AndroidRuntime(349): Caused by: java.lang.ClassCastException: android.widget.TextView
05-15 15:32:57.256: E/AndroidRuntime(349): at com.bignerdranch.android.quiz.CheatActivity.onCreate(CheatActivity.java:28)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 15:32:57.256: E/AndroidRuntime(349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-15 15:32:57.256: E/AndroidRuntime(349): ... 11 more
&#13;
答案 0 :(得分:0)
Logcat是你的朋友:
...
at com.bignerdranch.android.quiz.CheatActivity.onCreate(CheatActivity.java:28)
...
显然,您正在尝试将某个对象投射到某个类别,而该类别无法从中分配。检查CheatActivity.java,第28行。