android中的WebView在运行时出错

时间:2014-10-30 23:55:49

标签: android android-studio android-webview wear-os

我正在尝试为Android浏览器制作一个Web浏览器,我正在使用android studio。我的充气代码。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
           webview = (WebView)findViewById(R.id.wvBrowser);
            //this will set javascript enabled.
            webview.getSettings().setJavaScriptEnabled(true);
            //loading url to google.com
            webview.loadUrl("https://www.google.com");

            webview.getSettings().setLoadWithOverviewMode(true);
            webview.getSettings().setUseWideViewPort(true);

            webview.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
                }
            });

完整错误如下:

 android.view.InflateException: Binary XML file line #11: Error inflating class android.webkit.WebView
            at android.view.LayoutInflater.createView(LayoutInflater.java:620)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:57)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:131)
            at android.support.wearable.view.WatchViewStub.onMeasure(WatchViewStub.java:141)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2338)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1145)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1340)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1032)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5657)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5026)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:57)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:131)
            at android.support.wearable.view.WatchViewStub.onMeasure(WatchViewStub.java:141)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2338)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1145)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1340)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1032)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5657)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5026)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.UnsupportedOperationException
            at com.android.webview.nullwebview.NullWebViewFactoryProvider.createWebView(NullWebViewFactoryProvider.java:41)
            at android.webkit.WebView.ensureProviderCreated(WebView.java:2062)
            at android.webkit.WebView.setOverScrollMode(WebView.java:2120)
            at android.view.View.<init>(View.java:3461)
            at android.view.View.<init>(View.java:3517)
            at android.view.ViewGroup.<init>(ViewGroup.java:470)
            at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
            at android.webkit.WebView.<init>(WebView.java:498)
            at android.webkit.WebView.<init>(WebView.java:475)
            at android.webkit.WebView.<init>(WebView.java:455)
            at android.webkit.WebView.<init>(WebView.java:444)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:57)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:131)
            at android.support.wearable.view.WatchViewStub.onMeasure(WatchViewStub.java:141)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2338)
            at android.view.View.measure(View.java:16648)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1145)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1340)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1032)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5657)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5026)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)

我已经在我的xml中输入了正确的id作为wvBrowser,我在android Manifest xml文件中拥有用户权限。有人知道这个问题吗?提前致谢。

1 个答案:

答案 0 :(得分:3)

您无法在可穿戴设备上使用WebView,请在此处查看不可用功能列表: https://developer.android.com/training/wearables/apps/index.html

此外,您无法直接从可穿戴设备连接到互联网,它无法访问互联网,只能从配套手持设备接收数据。

编辑: 实际上,我只看到一种方法:

  1. 在掌上电脑中加载html页面并使用Wearable.DataApi将其传输到可穿戴设备
  2. 使用(可定制)第三方html库在可穿戴设备上显示页面。可能来自this answer的项目在这里很有用。