打开一个url时,WebView无法加载(android)

时间:2016-08-24 19:17:37

标签: javascript android android-fragments webview

抱歉我的英文。我有一个活动和一个片段(片段有webView)。在活动中我有项目菜单,如果我点击片段中的项目重新加载链接。每当我点击菜单中的项目时,我都会调用此代码

ItemMenuFragment fragment = new ItemMenuFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();

片段重新加载,然后他获得了webView的新链接。在片段加载页面html像这样

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(Global.URL_MAIN + ((MainActivity) getActivity()).getLink());

一切正常。但在html链接中我有个人资料页面,在这个个人资料页面我有按钮"加载图片",当我点击此按钮显示弹出窗口,此后所有项目菜单都无法加载网址。它显示白页和它的全部。

我的解决方法:

我认为,每次都需要重新加载我的片段,并在加载片段时添加此代码:

ItemMenuFragment fragment = new ItemMenuFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if(!fragmentTransaction.isEmpty()) fragmentTransaction.remove(fragment);
fragmentTransaction.add(R.id.frame_layout, fragment);
fragmentTransaction.commit();

它不起作用,然后我添加此代码:

ItemMenuFragment fragment = new ItemMenuFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.frame_layout, fragment);
fragmentTransaction.detach(fragment);
fragmentTransaction.attach(fragment);
fragmentTransaction.commit();

它也不起作用。然后我添加片段代码,干净的webView:

CookieSyncManager.createInstance(getActivity());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
webView.clearCache(true);
webView.clearHistory();

它也不起作用。我不知道如何解决我的问题。再一次关于我的问题:在webView中的弹出窗口之后,我无法再在webView中加载URL。它向我展示了白页。

UPD:

这是我项目的简单代码。我的左侧菜单动态创建,在左侧菜单中我有listView。我只有一个片段,每次单击菜单中的某个项目

时,此片段重新加载链接

MainActivity

public class MainActivity extends AppCompatActivity {

         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //menuItems - listview
                //item - object in menu(contains link and name)
                //adapterMenuItems adapter for lisView

                menuItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,  int position, long id) {
                        setLink(item.getUrl());
                        setFragment();
                        adapterMenuItems.notifyDataSetChanged();
                        if(menu.isMenuShowing()) menu.toggle();
                    }
                });
        }

    public void setFragment(){
            ItemMenuFragment fragment = new ItemMenuFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.frame_layout, fragment);
            fragmentTransaction.commit();
        }

            //set link for fragment
            public void setLink(String link){
                this.link = link;
            }

            //return link for fragment
            public String getLink(){
                return link;
            }
}

片段

public class ItemMenuFragment extends Fragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View containerView = inflater.inflate(R.layout.fragment_item_menu, container, false);

    webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);

    webView.loadUrl(Global.URL_MAIN + ((MainActivity) getActivity()).getLink());
}}

0 个答案:

没有答案