Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MainActivity$2符号出错,但实际上未与活动关联?_Java_Android_Android Studio - Fatal编程技术网

Java MainActivity$2符号出错,但实际上未与活动关联?

Java MainActivity$2符号出错,但实际上未与活动关联?,java,android,android-studio,Java,Android,Android Studio,我刚接触编码和android studio,目前正在做一项家庭作业,开发一款测验应用程序。应用程序运行正常,直到我出现以下错误: 多次定义com.example.quizapp.MainActivity$2类型: /用户/laurapaulino/桌面/移动 Computing/QuizApp2/app/build/intermediates/javac/debug/classes/com/example/quizapp/main活动$2 2.class、/Users/laurapaulino/

我刚接触编码和android studio,目前正在做一项家庭作业,开发一款
测验应用程序
。应用程序运行正常,直到我出现以下错误:

多次定义com.example.quizapp.MainActivity$2类型: /用户/laurapaulino/桌面/移动 Computing/QuizApp2/app/build/intermediates/javac/debug/classes/com/example/quizapp/main活动$2 2.class、/Users/laurapaulino/Desktop/Mobile Computing/QuizApp2/app/build/intermediates/javac/debug/classes/com/example/quizapp/main activity$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);
        }

    }
}

这就是我的主要活动课的样子。找不到错误所在

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

}

在实际类上没有错误的迹象,但我无法在模拟器上运行该程序。我希望能得到一些帮助。谢谢

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


希望这能起作用:)

实际错误是什么?它看起来就像代码。你为什么要编辑或试图编辑这个类?它是一个生成的类(最初是MainActivity中的第二个注释性内部类),您应该编辑实际的java文件MainActivity.java,而不是这个。这是我点击错误时打开的。我更新了问题,希望它更清楚,抱歉第一次发布!