0

I have a problem with android studio I created a fragment contains webview and progressbar the progress bar disappeared before the url in the web view loaded how to solve this problem? This is the java class file

public class FacebookPage extends Fragment {
    View myView;
    WebView myWebView;
    WebSettings webSettings;
    ProgressDialog progressDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView=inflater.inflate(R.layout.facebook_page,container,false);                
        return myView;
    };

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myWebView = (WebView) view.findViewById(R.id.webview);
        String url="https://www.facebook.com/TahrirLounge/";
        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String url) {
                super.onPageFinished(webView, url);
                view.findViewById(R.id.progressBar_facebook_page).setVisibility(View.GONE);
            }
        });

        myWebView.loadUrl(url);
        webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
}
}

This is the xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </WebView>

    <ProgressBar
        android:id="@+id/progressBar_facebook_page"
        style="?android:attr/progressBarStyle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:visibility="visible" />

</RelativeLayout>

and I also now have this error at the log:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mostafa.tahrirlounge, PID: 8952 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154) at android.app.AlertDialog.(AlertDialog.java:109) at android.app.ProgressDialog.(ProgressDialog.java:77) at android.app.ProgressDialog.show(ProgressDialog.java:110) at android.app.ProgressDialog.show(ProgressDialog.java:99) at com.mostafa.tahrirlounge.FacebookPage.(FacebookPage.java:24) at com.mostafa.tahrirlounge.MainActivity.onNavigationItemSelected(MainActivity.java:78) at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:156) at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822) at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969) at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342) at android.view.View.performClick(View.java:4757) at android.view.View$PerformClick.run(View.java:19757) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5233) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

1 Answers1

0

use this

public class FacebookPage extends Fragment {

    WebView webView;

    ProgressDialog progress;
    @Nullable
    @Override  
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View mainView = (View) inflater.inflate(R.layout.facebook_page, container, false);
        WebView webView = (WebView)mainView.findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);


        webView.loadUrl("https://www.facebook.com/TahrirLounge/");

        progress = ProgressDialog.show(this, "Loading",
                    "Loading", true);
        webView.setWebViewClient(new WebViewClient() {

           public void onPageFinished(WebView view, String url) {
                if (progress != null)
                    progress.dismiss();
            }
        });
        return mainView;


    }
Sahdeep Singh
  • 1,342
  • 1
  • 13
  • 34
  • There is a problem with "this" in the progressbar.show –  Aug 29 '17 at 23:15
  • use getActivity() instead of "this". – Sahdeep Singh Aug 30 '17 at 08:53
  • I changed it but now when I try to open the fragment the application closed –  Aug 30 '17 at 09:41
  • E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mostafa.tahrirlounge, PID: 21030 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference at ... –  Aug 30 '17 at 10:35
  • bro post the complete error(which is in red in log) after "reference at" so that i can better understand. You can use pastebin to share the log if you cant share in comments. – Sahdeep Singh Aug 30 '17 at 10:48
  • And there is One more thing... there is difference between progressBar and progressDialog.https://stackoverflow.com/questions/14339119/what-is-the-difference-between-progressbar-and-progressdialog – Sahdeep Singh Aug 30 '17 at 10:50
  • Alright, its getting worse.just upload the full source code. – Sahdeep Singh Aug 30 '17 at 13:00