java - MainActivity$2 符号出错但实际上与活动无关?
问题描述
我是编码和 android studio 的新手,目前正在做一个家庭作业来开发一个quiz app
. 该应用程序运行良好,直到我收到此错误:
com.example.quizapp.MainActivity$2 类型定义了多次:/Users/laurapaulino/Desktop/Mobile Computing/QuizApp2/app/build/intermediates/javac/debug/classes/com/example/quizapp/MainActivity$2 2.class, /Users/laurapaulino/Desktop/Mobile Computing/QuizApp2/app/build/intermediates/javac/debug/classes/com/example/quizapp/MainActivity$2.class
包 com.example.quizapp;
import android.view.View;
import android.view.View.OnClickListener;
class MainActivity$2 implements OnClickListener {
MainActivity$2(MainActivity this$0) {
this.this$0 = this$0;
}
public void onClick(View view) {
if (this.this$0.answer2.getText() == MainActivity.access$000(this.this$0)) {
MainActivity.access$108(this.this$0);
this.this$0.score.setText("Score: " + MainActivity.access$100(this.this$0));
MainActivity.access$300(this.this$0, this.this$0.r.nextInt(MainActivity.access$200(this.this$0)));
} else {
MainActivity.access$400(this.this$0);
}
}
}
这就是我的 Main Activity 类的样子。找不到错误在哪里...
package com.example.quizapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button answer1, answer2, answer3, answer4;
TextView score, question;
private Questions mQuestions = new Questions();
private String mAnswers;
private int mScore = 0;
private int mQuestionsLength = mQuestions.mQuestions.length;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r = new Random();
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer1.getText() == mAnswers){
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}
}
});
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer2.getText() == mAnswers){
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}
}
});
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer3.getText() == mAnswers){
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}
}
});
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (answer4.getText() == mAnswers){
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}
}
});
}
private void updateQuestion(int num ) {
question.setText(mQuestions.getQuestion(num));
answer1.setText(mQuestions.getChoice1(num));
answer2.setText(mQuestions.getChoice2(num));
answer3.setText(mQuestions.getChoice3(num));
answer4.setText(mQuestions.getChoice4(num));
mAnswers = mQuestions.getCorrectAnswer(num);
}
private void gameOver() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setMessage("Game Over! Your score is " + mScore + " points.")
.setCancelable(false)
.setPositiveButton("NEW GAME",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
})
.setNegativeButton("EXIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
第二个Java类:
package com.example.quizapp;
public class Questions {
public String mQuestions[] = {
"Who is the oldest Belcher kid?",
"What is Linda's sister's name?",
"Where do Linda's parents live?",
"What is Bob's favorite holiday?",
"Who is Bob's best friend?",
"Who is Bob's arch enemy?",
"What is Louise's favorite toy?",
"Who was Gene's first girlfriend?",
"Who did Gayle have a big crush on in high school?"
};
private String mChoices[][] = {
//"Tina", "Gayle", "Florida", "Thanksgiving", "Teddy", "Jimmy Pesto", "Kuchi Kopi", "Courtney", "Derek Dematopolis"
{"Tina", "Courtney", "Louise", "Gene"},
{"Gretchen", "Teddy", "Gayle", "Maria"},
{"New Jersey", "New York", "King's Head Island", "Florida"},
{"Halloween", "Thanksgiving", "Christmas", "Easter"},
{"Mr. Fischoeder", "Gayle", "Teddy", "Hugo"},
{"Jimmy Pesto", "Mr. Fischoeder", "Ron", "Randy"},
{"Teddy Bear", "Rare Pony", "Kuchi Kopi", "Tricycle"},
{"Millie", "Tammy", "Jocelyn", "Courtney"},
{"Gretchen", "Felix Fischoeder", "Dr. Yap", "Derek Dematopolis"},
};
private String mCorrectAnswers[] = {"Tina", "Gayle", "Florida", "Thanksgiving", "Teddy", "Jimmy Pesto", "Kuchi Kopi", "Courtney", "Derek Dematopolis"};
public String getQuestion (int a) {
String question = mQuestions[a];
return question;
}
public String getChoice1(int a) {
String choice = mChoices[a][0];
return choice;
}
public String getChoice2(int a) {
String choice = mChoices[a][1];
return choice;
}
public String getChoice3(int a) {
String choice = mChoices[a][2];
return choice;
}
public String getChoice4(int a) {
String choice = mChoices[a][3];
return choice;
}
public String getCorrectAnswer(int a) {
String answer = mCorrectAnswers[a];
return answer;
}
}
实际类中没有任何错误迹象,但我无法在模拟器上运行该程序。我会很感激一些帮助。谢谢!
解决方案
我遇到了同样的问题.....解决方案:单击构建->清理项目,然后单击构建->重建项目。
希望这会奏效:)
推荐阅读
- hyperledger-fabric - 如何使用 fabric-sdk-go 在 Hyperledger Fabric 中异步发送事务或批量发送
- mingw - [Qemu][Cross Compile for windows][Mingw] 启用curses时找不到langinfo.h
- javascript - 如何检查动态添加选项到选择框是否完成?
- php - Laravel GroupBy 在 mysql 中工作,但在 MariaDB 中抛出错误
- ubuntu - 为什么在 ubuntu 16.04 启动 libvirtd 服务失败?
- c# - 运行时浮点计算与评估计算
- scala - 为什么 Spark 在调用另一个 udf 而不是原始列之前转换 udf 生成的列?
- html - 默认字体从何而来?
- php - 如何在从数据库中提取的已发布图像上使用灯箱
- arrays - 使用转义字符创建二维数组