在android studio

时间:2017-08-29 14:46:53

标签: java android webview progress-bar

我有一个android studio的问题我创建了一个包含webview和progressbar的片段,进度条消失之前在web视图中的url加载如何解决这个问题? 这是java类文件

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);
}
}

这是xml文件:

<?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>

我现在也在日志中出现此错误:

E / AndroidRuntime:致命异常:主要                                                                         过程:com.mostafa.tahrirlounge,PID:8952                                                                         java.lang.NullPointerException:尝试调用虚方法&#39; android.content.res.Resources $ Theme android.content.Context.getTheme()&#39;在null对象引用上                                                                             在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)                                                                             在android.app.AlertDialog。(AlertDialog.java:109)                                                                             在android.app.ProgressDialog。(ProgressDialog.java:77)                                                                             在android.app.ProgressDialog.show(ProgressDialog.java:110)                                                                             在android.app.ProgressDialog.show(ProgressDialog.java:99)                                                                             在com.mostafa.tahrirlounge.FacebookPage。(FacebookPage.java:24)                                                                             at com.mostafa.tahrirlounge.MainActivity.onNavigationItemSelected(MainActivity.java:78)                                                                             在android.support.design.widget.NavigationView $ 1.onMenuItemSelected(NavigationView.java:156)                                                                             在android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)                                                                             在android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)                                                                             在android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)                                                                             在android.support.design.internal.NavigationMenuPresenter $ 1.onClick(NavigationMenuPresenter.java:342)                                                                             在android.view.View.performClick(View.java:4757)                                                                             在android.view.View $ PerformClick.run(View.java:19757)                                                                             在android.os.Handler.handleCallback(Handler.java:739)                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                             在android.os.Looper.loop(Looper.java:135)                                                                             在android.app.ActivityThread.main(ActivityThread.java:5233)                                                                             at java.lang.reflect.Method.invoke(Native Method)                                                                             在java.lang.reflect.Method.invoke(Method.java:372)                                                                             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:898)                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

1 个答案:

答案 0 :(得分:0)

使用此

CharSequence.toString