WebView从另一个类加载URL

时间:2014-11-26 16:26:10

标签: java android xml eclipse

我想在activity_main.xml中使用名为refresh的actionbar按钮在单击时加载url。 webview位于fragmenttab1.xml

我该怎么做?

修改

MainActivity.java中操作栏菜单的代码

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_refresh) {
            FragmentTab1 rld1 = new FragmentTab1();
            rld1.webViewRefresh();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
FragmentTab1.xml中的

public class FragmentTab1 extends Fragment {

    WebView webView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // get the url to open
        // set up the WebView
        webView = (WebView) getView().findViewById(R.id.webView);
        webView.setWebViewClient(new MyBrowser());
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);  
        webView.loadUrl("http://192.168.1.4/index.html");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.fragmenttab1, container, false);
        return view;
    }
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           view.loadUrl(url);
           return true;
        }
    }

    @Override
    public void onPause() {
        super.onPause();  // Always call the superclass method first
        webViewRefresh();
        //webView.loadUrl("http://192.168.1.4/index.html");
    }

    public void webViewRefresh() {
        webView.loadUrl("http://192.168.1.4/index.html");
    }


}

logcat的

11-27 01:02:20.555: E/AndroidRuntime(31088): FATAL EXCEPTION: main
11-27 01:02:20.555: E/AndroidRuntime(31088): java.lang.NullPointerException
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.cn1304w.munch.FragmentTab1.webViewRefresh(FragmentTab1.java:53)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.cn1304w.munch.MainActivity.onOptionsItemSelected(MainActivity.java:44)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.app.Activity.onMenuItemSelected(Activity.java:2640)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:373)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.view.View.performClick(View.java:4475)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.view.View$PerformClick.run(View.java:18786)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.os.Handler.handleCallback(Handler.java:730)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.os.Looper.loop(Looper.java:137)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at android.app.ActivityThread.main(ActivityThread.java:5419)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at java.lang.reflect.Method.invokeNative(Native Method)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at java.lang.reflect.Method.invoke(Method.java:525)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
11-27 01:02:20.555: E/AndroidRuntime(31088):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您需要引用或使用FragmentManager获取片段。然后你需要调用片段中的方法来刷新webview。

在您的活动中的onOptionsItemSelected中:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    case R.menu.refresh:
          fragmentReference.webViewRefresh();
    return super.onOptionsItemSelected(item);
}

关于片段方法:

public void webViewRefresh() {
    this.webview.loadUrl(this.webURL);
}

答案 1 :(得分:0)

java.lang.NullPointerException
at com.cn1304w.munch.FragmentTab1.webViewRefresh(FragmentTab1.java:53)

查看你的FragmentTab1.java第53行,你正在对一个NULL的对象做一些事情,因此NullPointerException

我敢打赌你的webView对象是NULL。

我认为您的以下代码应位于onViewCreated(),而不是onActivityCreated()

@Override
public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    webView = (WebView) view.findViewById(R.id.webView);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);  
    webView.loadUrl("http://192.168.1.4/index.html");
}

使用调试器跟踪对象的值并修复错误。

答案 2 :(得分:0)

使用ButterKnife和Otto,这实际上非常简单:

public class WebViewFragment extends Fragment
{
   private Handler webViewHandler = new Handler(Looper.myLooper());

   public static class RefreshEvent
   {
   }

   private enum WebViewStateHolder
   {
        INSTANCE;

        private Bundle bundle;

        public void saveWebViewState(WebView webView)
        {
            bundle = new Bundle();
            webView.saveState(bundle);
        }

        public Bundle getBundle()
        {
            return bundle;
        }
    }

    @InjectView(R.id.webview)
    public WebView webView;

    public WebViewFragment()
    {
    }

    @Override
    public void onResume()
    {
        super.onResume();
        SingletonBus.INSTANCE.getBus().register(this);
    }

    @Override
    public void onPause()
    {
        SingletonBus.INSTANCE.getBus().unregister(this);
        WebViewStateHolder.INSTANCE.saveWebViewState(webView);
        super.onPause();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ButterKnife.inject(this, rootView);

        WebSettings webSettings = webView.getSettings();
        ....
        if(WebViewStateHolder.INSTANCE.getBundle() == null)
        {
            webView.loadUrl("http://192.168.1.4/index.html");
        }
        else
        {
            webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
        }
        return rootView;        
    }

    @Subscribe
    public void handleRefreshEvent(RefreshEvent e)
    {
        webViewHandler.post(new Runnable()
        {
           @Override
           public void run()
           {
               webView.loadUrl("http://192.168.1.4/index.html");
           }
        }
    }
}

public enum SingletonBus
{
    INSTANCE;

    private Bus bus;

    private SingletonBus()
    {
        this.bus = new Bus(ThreadEnforcer.ANY);
    }

    public Bus getBus()
    {
        return bus;
    }
}

然后你可以做

    if (id == R.id.action_refresh) {
        SingletonBus.INSTANCE.getBus().post(new WebViewFragment.RefreshEvent());
        return true;
    }
    return super.onOptionsItemSelected(item);