这是我的quizactivity.java,我的xml没有任何错误,所以我不会上传这些,除非你需要它,如果你需要,请告诉我。它还说,公开的无效的onStart停止销毁简历应该在} //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/dalvikvm(382):onCreate(捆绑)调用04-27 22:34:29.597: D/AndroidRuntime(382):关闭VM 04-27 22:34:34:29.597: W/dalvikvm(382):threadid=1:线程发出未知异常(group=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):at android.os.Handler.dispatchMessage(Handler.java:99) 04-27 22:34:29.617: E/AndroidRuntime(382):在android.os.Looper.loop( android.app.ActivityThread.main(ActivityThread.java:4025) ) 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 java.lang.reflect.Method.invokeNative(原生方法) 04-27 22:34:29.617: E/AndroidRuntime(382):java.lang.reflect.Method.invoke(Method.java:491) 04-27 22:34:29.617E/AndroidRuntime(382):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 04-27 22:34:29.617: E/AndroidRuntime(382):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 04-27 22:34:29.617: E/AndroidRuntime(382):at dalvik.system.NativeStart.main(原生方法) 04-27 22:34:29.617: E/AndroidRuntime(382):由: java引起。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):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多11,04-27 22:34:39.047: I/Process(382):发送信号。PID: 382 SIG: 9
发布于 2014-04-27 23:01:55
Luiggi在注释中是正确的,堆栈跟踪提供了您需要的关于错误的所有信息。特别是这一行:
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扩展了ImageView,而不是Button。被抛出一个ClassCastException是因为您试图将一件事情转化为它不是的东西。
看看您的activity_quiz.xml文件,看看哪个视图是ImageButton。您需要将活动中的类型更新为ImageButton以解决此问题。
祝Android开发好运,它可能会很有趣!:)
https://stackoverflow.com/questions/23329933
复制相似问题