另一个Big Nerd Ranch Android Crash错误

时间:2014-04-27 22:39:37

标签: java android eclipse crash

继承我的quizactivity.java我的xml没有任何错误所以除非你需要,我不会上传那些,请告诉我你是否这样做。另外它说onStart Stop Destroy Resume Ect的公共空白应该在} //end onCreate(Bundle)之下,但是我找不到它的位置,或者它是否应该位于那里,有没有办法可以搜索某些文字或短语在未来的quizactivity.java中。

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

}

这是我刚刚复制并粘贴一个特定的LogCat到我的包

04-27 22:34:29.026:D / QuizActivity(382):onCreate(Bundle)调用 04-27 22:34:29.597:D / AndroidRuntime(382):关闭虚拟机 04-27 22:34:29.597:W / dalvikvm(382):threadid = 1:线程退出未捕获异常(组= 0x40014760) 04-27 22:34:29.617:E / AndroidRuntime(382):致命异常:主要 04-27 22:34:29.617:E / AndroidRuntime(382):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.bignerdranch.android.geoquiz / com.bignerdranch.android.geoquiz.QuizActivity}:java.lang中.ClassCastException:android.widget.ImageButton无法强制转换为android.widget.Button 04-27 22:34:29.617:E / AndroidRuntime(382):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 04-27 22:34:29.617:E / AndroidRuntime(382):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 04-27 22:34:29.617:E / AndroidRuntime(382):在android.app.ActivityThread.access $ 1500(ActivityThread.java:122) 04-27 22:34:29.617:E / AndroidRuntime(382):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1002) 04-27 22:34:29.617:E / AndroidRuntime(382):在android.os.Handler.dispatchMessage(Handler.java:99) 04-27 22:34:29.617:E / AndroidRuntime(382):在android.os.Looper.loop(Looper.java:132) 04-27 22:34:29.617:E / AndroidRuntime(382):在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):引起:java.lang.ClassCastException:android.widget.ImageButton无法强制转换为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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 04-27 22:34:29.617:E / AndroidRuntime(382):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712) 04-27 22:34:29.617:E / AndroidRuntime(382):......还有11个 04-27 22:34:39.047:I / Process(382):发送信号。 PID:382 SIG:9

1 个答案:

答案 0 :(得分:2)

Luiggi在评论中是正确的,stacktrace提供了有关错误的所有信息。特别是这一行:

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

在OnCreate方法的某处,您试图将ImageButton强制转换为Button,这是无效的。根据Android开发人员文档,ImageButton extends ImageView, not Button.因此,正在抛出 ClassCastException ,因为您试图将一件事转变为不是它的东西。

查看 activity_quiz.xml 文件,查看哪个视图是ImageButton。您需要将活动中的类型更新为ImageButton才能解决此问题。

Android开发好运,它可以很有趣! :)