简体   繁体   中英

How can i pass data into my fragments to my view?

hello im new to android studio I just created my fragments and want to create a button that randomizes a number. it worked before i did the fragments and now i simply doesnt know how to get in the same code i used before but.. it doesnt seem to find the id of my text view or my button

package com.example.thesapplikation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class RandomFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable  ViewGroup container,@Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_home,container,false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Random myRandom = new Random();

        Button buttonGenerate = (Button) findViewById(R.id.generate);
        final TextView textGenerateNumber=(TextView)findViewById(R.id.generatenumber);

        buttonGenerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textGenerateNumber.setText(String.valueOf(myRandom.nextInt(100)));
            }
        });


    }
}

It's same answer as @Mike said. Fragment's life cycle methods are different then that of an Activity, you can get more info from here .

OnCreate is called before the OnCreateView so you dont have access to the fragment's view inside the OnCreate method. Move everything you doing inside OnViewCreated . So your fragment code will be

public class RandomFragment extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable  ViewGroup container,@Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home,container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button buttonGenerate = (Button) view.findViewById(R.id.generate);
        final TextView textGenerateNumber=(TextView)view.findViewById(R.id.generatenumber);

        final Random myRandom = new Random();
        buttonGenerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textGenerateNumber.setText(String.valueOf(myRandom.nextInt(100)));
            }
        });

    }
}

Your code should be inside onCreateView(). Here, what it should look like to work.

public class RandomFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable  ViewGroup container,@Nullable Bundle savedInstanceState) {

        final Random myRandom = new Random();

        View root = inflater.inflate(R.layout.fragment_home,container,false);

        Button buttonGenerate =  root.findViewById(R.id.generate);
        final TextView textGenerateNumber = root.findViewById(R.id.generatenumber);

        buttonGenerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textGenerateNumber.setText(String.valueOf(myRandom.nextInt(100)));
            }
        });

        return root;
    }

}

I hope this works, works for me BTW. Best of luck! ☺️

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