Question

Heres my quizactivity.java my xml's do not have any errors so I will not upload those unless you need it, Please tell me if you do. Also it says that the public void onStart Stop Destroy Resume Ect should be under } //end onCreate(Bundle) but I cant find were that is located or if it should even be located in there, is there any way I could search for certain text or phrase within quizactivity.java in the future.

 package com.bignerdranch.android.geoquiz; 

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends ActionBarActivity {

    private static final String TAG = "QuizActivity";

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mPrevButton;
    private Button mNextButton;
    private TextView mQuestionTextView;


    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;

    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 (userPressedTrue == answerIsTrue) {
           messageResId = R.string.correct_toast;
       } else {
           messageResId = R.string.incorrect_toast;    
       }

       Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
       .show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate(Bundle) called");
        setContentView(R.layout.activity_quiz); 

        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);

        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 = (Button)findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });
        mPrevButton = (Button)findViewById(R.id.prev_button);
        mPrevButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 4) % mQuestionBank.length;
                updateQuestion();

            }
        });

                updateQuestion();

            } 

    @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");
    }

    @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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_quiz, container,
                    false);
            return rootView;
        }
    }

}

Here is the LogCat I just copied and pasted the one particular to my package

04-27 22:34:29.026: D/QuizActivity(382): onCreate(Bundle) called 04-27 22:34:29.597: D/AndroidRuntime(382): Shutting down VM 04-27 22:34:29.597: W/dalvikvm(382): threadid=1: thread exiting with uncaught exception (group=0x40014760) 04-27 22:34:29.617: E/AndroidRuntime(382): FATAL EXCEPTION: main 04-27 22:34:29.617: E/AndroidRuntime(382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button 04-27 22:34:29.617: E/AndroidRuntime(382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.app.ActivityThread.access$1500(ActivityThread.java:122) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.os.Handler.dispatchMessage(Handler.java:99) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.os.Looper.loop(Looper.java:132) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.app.ActivityThread.main(ActivityThread.java:4025) 04-27 22:34:29.617: E/AndroidRuntime(382): at java.lang.reflect.Method.invokeNative(Native Method) 04-27 22:34:29.617: E/AndroidRuntime(382): at java.lang.reflect.Method.invoke(Method.java:491) 04-27 22:34:29.617: E/AndroidRuntime(382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 04-27 22:34:29.617: E/AndroidRuntime(382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 04-27 22:34:29.617: E/AndroidRuntime(382): at dalvik.system.NativeStart.main(Native Method) 04-27 22:34:29.617: E/AndroidRuntime(382): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button 04-27 22:34:29.617: E/AndroidRuntime(382): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:81) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 04-27 22:34:29.617: E/AndroidRuntime(382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712) 04-27 22:34:29.617: E/AndroidRuntime(382): ... 11 more 04-27 22:34:39.047: I/Process(382): Sending signal. PID: 382 SIG: 9

Was it helpful?

Solution

Luiggi is correct in the comments, the stacktrace provides all the info you need about the error. Particularly this line:

22:34:29.617: E/AndroidRuntime(382): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button

Somewhere in your OnCreate method you are trying to cast an ImageButton as a Button, this is invalid. According to the Android Developer Docs, ImageButton extends ImageView, not Button. Thus, a ClassCastException is being thrown because you are trying to turn one thing into something that it is not.

Take a look at your activity_quiz.xml file and see which view is an ImageButton. You'll need to update its type in your activity to ImageButton to fix this issue.

Good luck with Android Development, it can be pretty fun! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top