0

I want to refresh fragment by clicking retry button in onCreateView. If my internet is disconnected it must return fragment_no_internet_error layout. There is a button to reload fragment.

It gives me the error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ir.hamedanmelk.hamedanmelk, PID: 21310
    java.lang.NullPointerException: Attempt to read from field 'androidx.fragment.app.FragmentManager androidx.fragment.app.Fragment.mFragmentManager' on a null object reference
        at androidx.fragment.app.BackStackRecord.detach(BackStackRecord.java:220)

my OnCreateView:

@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v;
    CheckConnectivity checkConnectivity=new CheckConnectivity();
    if(!checkConnectivity.isNetworkAvailable(getActivity())) {
        v=inflater.inflate(R.layout.fragment_no_internet_error, container, false);
        Button retrybutton = (Button)v.findViewById(R.id.no_internet_fragment_button_retry);
        retrybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Fragment frg = getActivity().getSupportFragmentManager().findFragmentById(R.id.HelpFragment);
                final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                ft.detach(frg);
                ft.attach(frg);
                ft.commit();
            }
        });
        return v;
    }


    v=inflater.inflate(R.layout.fragment_f_a_q, container, false);
    mwebview = (WebView) v.findViewById(R.id.webview_f_a_q);
    WebSettings webSettings=mwebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    //TODO Change FontFace https://stackoverflow.com/questions/3900658/how-to-change-font-face-of-webview-in-android
    mwebview.setWebViewClient(new WebViewClient(){

    return v;
 }
}
Daan Seuntjens
  • 880
  • 1
  • 18
  • 37
Jalal Yadegar
  • 21
  • 2
  • 6

2 Answers2

0

Try to use this code. Becuase I think the issue is with API level

FragmentTransaction transaction = mActivity.getFragmentManager()
                    .beginTransaction();
            if (Build.VERSION.SDK_INT >= 26) {
                transaction.setReorderingAllowed(false);
            }
            transaction.detach(this).attach
                    (this).commit();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sam
  • 241
  • 3
  • 7
  • What API level you are using? – Sam Oct 20 '20 at 08:47
  • compileSdkVersion 29 – Jalal Yadegar Oct 20 '20 at 09:05
  • Okay, if the problem is still coming use another way, Like call the activity again from the fragment. what this gonna do is when you call activity the activity of runs oncreate method and your fragment will be called and it is the updated fragment. you can pass data from a fragment to that activity and use if-else statement for again calling the fragment if you don't wanna call that fragment every time when that activity is called. – Sam Oct 20 '20 at 09:08
  • i'll try that,تشکر – Jalal Yadegar Oct 20 '20 at 09:16
0

try this. in your fragment

 ((MainActivity)getActivity()).reload() 

in your MainActivity

void reload(){
    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // R.id.fragment_layout is your fragment container view
    ft.replace(R.id.fragment_layout, instanceFragment);
    ft.commit()
}
Ramy Ibrahim
  • 656
  • 4
  • 19