簡體   English   中英

為什么我不能從另一個類訪問方法

[英]why i can't access to method from another class

我正在嘗試從Firebase獲取問題並將其顯示在TextView 首先,我有GameManager類,在這個class我得到了所有問題並將它們添加到ArrayList

public class GameManager {

    Context context;
    ArrayList<MyQuestion> listquestions = new ArrayList<>();
    private MyQuestion[] questions;
    private String[][] options;
    private int[] answers;
    private DatabaseReference mDatabase;


    public GameManager(Context context) {
        getQuestions();


/*new String[]{"In which country were the first Olympic Games held?", "Which popular american TV show holds the record for most viewers for a finale?", "Where did Chess originate from?", "Which version of Android did not support phones?"};*/
        options = new String[][]{{"Greece", "Spain", "USA", "Russia"},
                {"Cheers", "Friends", "M*A*S*H", "Seinfeld"},
                {"China", "India", "UK", "Germany"},
                {"Donut", "Eclair", "Gingerbread", "Honeycomb"}};
        // 1 - first option, 4 - fourth option
        answers = new int[]{1, 3, 2, 4};
        this.context = context;
        listquestions = new ArrayList<>();
    }

    public QuestionModel generateRandomQuestion(long previousId) {
        Random random = new Random();
        int index = random.nextInt(questions.length);
        while (index == previousId) {
            index = random.nextInt(questions.length);
        }
        QuestionModel testQuestion = new QuestionModel();
        testQuestion.setId(index);
        testQuestion.setQuestion(questions[index].getQuestion());
        List<OptionModel> options = new ArrayList<OptionModel>();

        OptionModel option1 = new OptionModel();
        option1.setOptionId(1);
        option1.setOptionString(this.options[index][0]);
        option1.setCorrectAnswer(answers[index] == 1);

        OptionModel option2 = new OptionModel();
        option2.setOptionId(2);
        option2.setOptionString(this.options[index][1]);
        option2.setCorrectAnswer(answers[index] == 2);
         ....

        options.add(option1);
        options.add(option2);
        .....
        testQuestion.setOptions(options);
        return testQuestion;
    }

    public void getQuestions() {
        mDatabase = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference mUser = mDatabase.child("Quizy").child("ChallengeQuestions").child("Questions");
        mUser.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    MyQuestion user = postSnapshot.getValue(MyQuestion.class);
                    assert user != null;
                    listquestions.add(user);
                }
                questions = listquestions.toArray(new MyQuestion[listquestions.size()]);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

}


public void generateNewQuestion() {
        long previousQuestionId = -1;
        try {
            previousQuestionId = mGameModel.getQuestion().getId();
        } catch (NullPointerException e) {
            previousQuestionId = -1;
        }
        GameManager gameManager = new GameManager(getApplicationContext());
        mGameModel.setQuestion(gameManager.generateRandomQuestion(previousQuestionId));
        mGameModel.getGameMaster().setDidPlayerAnswer(false);
        mGameModel.getGameMaster().setPlayerAnswerCorrect(false);
        mGameModel.getGameSlave().setDidPlayerAnswer(false);
        mGameModel.getGameSlave().setPlayerAnswerCorrect(false);
    }

我的問題是我無法使用此方法generateRandomQuestion 如果我想動態獲取問題(使用Firebase ),則無法訪問它,但是當我以static方式設置問題時,我不知道為什么。

如果您嘗試從另一個類調用此方法,則該方法將無效,因為您將該方法設為private這意味着只能在該類本身內對其進行訪問。 您不能從另一個類調用此方法。

如果此方法不需要private考慮將其更改為publicprotected

這也可能會有所幫助: 公共,私有和受保護的區別是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM