gpt4 book ai didi

java - 另一个 Big Nerd Ranch Android 崩溃错误

转载 作者:行者123 更新时间:2023-12-02 05:53:29 26 4
gpt4 key购买 nike

这是我的 quizactivity.java,我的 xml 没有任何错误,所以除非您需要,否则我不会上传这些错误,如果您需要,请告诉我。它还说 public void 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:无法启动 Activity ComponentInfo {com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}:java.lang .ClassCastException:android.widget.ImageButton 无法转换为 android.widget.Button04-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):在java.lang.reflect.Method.invokeNative( native 方法)04-27 22:34:29.617:E/AndroidRuntime(382):在java.lang.reflect.Method.invoke(Method.java:491)04-27 22:34:29.617: E/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):在dalvik.system.NativeStart.main( native 方法)04-27 22:34:29.617:E/AndroidRuntime(382):引起:java.lang.ClassCastException:android.widget.ImageButton无法转换为android.widget.Button04-27 22:34:29.617:E/AndroidRuntime(382):在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

最佳答案

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 extends ImageView, not Button.因此,ClassCastException 会被抛出,因为您试图将一件事变成另一件事。

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

祝 Android 开发好运,它会非常有趣! :)

关于java - 另一个 Big Nerd Ranch Android 崩溃错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23329933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com