I have a question for Android Fragment.

Asked 2 years ago, Updated 2 years ago, 113 views

I use Volley and Gson to get the data. I want to transfer the data I received from the main activity to the Fragment containing ViewPager I put data as Budle in Viewpager Fragment and receive it as getArgument at the time Viewpager Fragment becomes OnCreateView, but I keep getting nullpoint errors... Why is that?

public void processRespons(String response) {
    Gson gson = new Gson();

    ResponseInfo info = gson.fromJson(response, ResponseInfo.class);
    MovieList movieList = gson.fromJson(response, MovieList.class);
    if (info.code == 200) {
        MovieInfo movieInfo = movieList.result.get(1);
        String title = movieInfo.title;
        Bundle bundle = new Bundle();
        bundle.putString("title", title);

        ViewPager1 viewPager = new ViewPager1();
        viewPager.setArguments(bundle);

    }
}


public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = (ViewGroup) inflater.inflate(R.layout.view_pager1, container, false);

    Button button = rootView.findViewById(R.id.viewpager1_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bundle bundle = getArguments();
            String title = bundle.getString("title");

            TextView text = rootView.findViewById(R.id.viewpager1_title);
            text.setText(title);


        }
    });

fragment android

2022-09-22 20:25

1 Answers

ViewPager1 viewPager1();
viewPager.setArguments(bundle);

Are we going to create a new fragment object and finish with setArguments? Even if you do setArguments, I think you should put it in the fragment object bound to ViewPager. If you look at the two lines of code in that situation, it seems to be meaningless

If the number of fragments is set, you can send the value to the fragment and update it directly, or you can request http from the fragment in the first place. There is also a way to implement it in response to notifyDataSetChanged() in the fragment adapter. I don't know the whole flow, so it's hard to give you detailed advice


2022-09-22 20:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.