首页 > 解决方案 > 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;
    }

}

实际类中没有任何错误迹象,但我无法在模拟器上运行该程序。我会很感激一些帮助。谢谢!

标签: javaandroidandroid-studio

解决方案


我遇到了同样的问题.....解决方案:单击构建->清理项目,然后单击构建->重建项目。

希望这会奏效:)


推荐阅读