简体   繁体   中英

Not able to create a button in main activity from a fragment?

I would like to create a button in MainActivity.java for example, that is in a different fragment.

To be a bit more clear, I have an Activity that holds 3 fragments, I want to create a button from fragment 2 in that Activity. But when trying to initialize them, I get a null pointer exception.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);


    if (savedInstanceState == null) {

        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.id.scoreFragment, new ScoreFragment());
        fragmentTransaction.add(R.id.questionFragment, new QuestionFragment());
        fragmentTransaction.add(R.id.navigationFragment, new NavigationFragment());

        //creates the questions
        questions = new ArrayList<Question>();
        currentQuestion = new Question("correct");
        questions.add(currentQuestion);
        fragmentTransaction.commit();

//here is where the null pointer exception appears.
        QuestionFragment q = (QuestionFragment)    fragmentManager.findFragmentById(R.id.questionFragment);
        //View v = q.;
        initialize(q);
    }
    //Button nextQuestionButton = (Button) findViewById(R.id.next);
    //nextQuestionButton.setEnabled(false);

}


/* WHY DOESNT IT WORK? */
private void initialize(QuestionFragment q){

    View v=q.getView();

    buttons[0] = (Button) v.findViewById(R.id.button1);
    buttons[1] = (Button) v.findViewById(R.id.button2);
    buttons[2] = (Button) v.findViewById(R.id.button3);
    buttons[3] = (Button) v.findViewById(R.id.button4);


    //loads possible answers from array
    Resources res = getResources();
    String[] answers = res.getStringArray(R.array.questionBank);


    //creates a random number between 0 and 3 that button is correct and the others are incorrect
    Random rand = new Random();
    int correct = rand.nextInt(4); 


    buttons[correct].setText(answers[0]);
    buttons[(correct + 1) % 4].setText(answers[1]);
    buttons[(correct + 2) % 4].setText(answers[2]);
    buttons[(correct + 3) % 4].setText(answers[3]); 
    }

I got it to work if I create the buttons in the QuestionFragment in onCreateView but I would like to try to make it work in QuizActivity , which holds the 3 fragments.

How can I do this?

Try overriding onCreateView in the QuestionFragment class, and do your QuestionFragment UI initialization there rather than trying to manage it from the Activity level.

Also, in your example above, if fragmentManager.findFragmentById(R.id.questionFragment) is returning null then that means that the fragment hasn't been added/doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM