Modularization within the fragment...

Asked 2 years ago, Updated 2 years ago, 120 views

If you do the fragment module and run it, it turns off when you run the app in Emule What is the error?

public class ChannelBuskerSchedule extends Fragment {

int test__schedule=5;

public ChannelBuskerSchedule(){
    // // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.activity_channel_busker_schedule, container, false);

    for (int scheduleCount=0; scheduleCount<test__schedule; scheduleCount++) {
        addSchedulesch(inflater);
        Log.e ("number of statements for", String.valueOf(scheduleCount)));
    }

    return v;
}

public void addSchedulesch(final LayoutInflater inflater){
    final ImageButton dropdownBtn = (ImageButton)getView().findViewById(R.id.dropdown_sch);
    final LinearLayout scheduleBox = (LinearLayout)getView().findViewById(R.id.addSchedule_sch);

    if (test__schedule > 1 ){
        dropdownBtn.setVisibility(View.VISIBLE);
        View list = inflater.inflate(R.layout.schedule_list,scheduleBox,false);
        if(list.getParent()!= null)
            ((ViewGroup)list.getParent()).removeView(list);
        scheduleBox.addView(list);
        scheduleBox.setVisibility(View.GONE);
        dropdownBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                scheduleBox.setVisibility(View.VISIBLE);
                dropdownBtn.setVisibility(View.INVISIBLE);
            }
        });
    }
}

}

android android-studio module fragment

2022-09-22 20:09

2 Answers

The content view of the fragment is bound to the view returned from the onCreateView. If you look at the code logic you wrote, addSchedulesch() is called before returning the view inflated in onCreateView, but it seems that NullPointerException will occur because the view is still null at getView() within this function. You can do it after the onCreateView() is over and the content view is set, or you can do it with the v variable that has been verified and returned.

We recommend that you override onActivityCreated() and handle future routines within that function.


2022-09-22 20:09

An error occurs because you did getView before returning the view that was printed in onCreateView. As you mentioned above, call the method in onActivityCreated().


2022-09-22 20:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.