java–WebView loadurl() –android app

java–WebView loadurl() –android app,第1张

概述当我点击webview页面内的按钮时出错.因此,当我点击按钮时,它将更改为谷歌网站.以下是代码和错误:–MainActivity.javapackagecom.mt.nad.testwebapp;importandroid.content.Context;importandroid.support.v7.app.ActionBarActivity;importandroid.os.Bundle;importa

当我点击webvIEw页面内的按钮时出错.因此,当我点击按钮时,它将更改为谷歌网站.以下是代码和错误: –

MainActivity.java

package com.mt.nad.testwebapp;import androID.content.Context;import androID.support.v7.app.ActionBaractivity;import androID.os.Bundle;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.webkit.JavaScriptInterface;import androID.webkit.WebSettings;import androID.webkit.WebVIEw;import androID.webkit.WebVIEwClIEnt;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class MainActivity extends ActionBaractivity {private WebVIEw webC;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    webC = (WebVIEw)findVIEwByID(R.ID.webVIEw1);    webC.addJavaScriptInterface(new JavaScriptInterface(), "CallJavaAdapter");    webC.setWebVIEwClIEnt(new WebVIEwClIEnt());    WebSettings webS = webC.getSettings();    webS.setJavaScriptEnabled(true);    webC.loadUrl("http://10.0.2.2/test-java-adapter/");}   private class JavaScriptInterface{    JavaScriptInterface() {    }    @JavaScriptInterface    public voID gotoSite() {        //Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();        webC.clearCache(true);        webC.loadUrl("http://Google.com");    }}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.menu_main, menu);    return true;}@OverrIDepublic 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();    //noinspection SimplifiableIfStatement    if (ID == R.ID.action_settings) {        return true;    }    return super.onoptionsItemSelected(item);}}

activity_main.xml中

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent"androID:layout_height="match_parent" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" androID:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><TextVIEw androID:text="@string/hello_world" androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:ID="@+ID/textVIEw" /><WebVIEw    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:ID="@+ID/webVIEw1"    androID:layout_alignParentleft="true"    androID:layout_alignParentStart="true" /></relativeLayout>

AndroIDManifest.xml中

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"package="com.mt.nad.testwebapp" ><uses-permission androID:name="androID.permission.INTERNET"></uses-permission><application    androID:allowBackup="true"    androID:icon="@drawable/ic_launcher"    androID:label="@string/app_name"    androID:theme="@style/Apptheme" >    <activity        androID:name=".MainActivity"        androID:label="@string/app_name" >        <intent-filter>            <action androID:name="androID.intent.action.MAIN" />            <category androID:name="androID.intent.category.LAUNCHER" />        </intent-filter>    </activity></application></manifest>

的index.HTML

<!DOCTYPE HTML><HTML><head>  <Meta charset="UTF-8">  <Meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">  <Meta name="description" content="">  <Meta name="vIEwport" content="wIDth=device-wIDth, initial-scale=1">  <Title>Test Only</Title>  <link rel="stylesheet" href=""></head><body><div><input type="button" value="Go To Site" onClick="window.CallJavaAdapter.gotoSite()"></div><script src="http://code.jquery.com/jquery-1.11.1.min.Js"></script></body></HTML>

logcat的

12-29 03:54:04.099    2793-2838/com.mt.nad.testwebapp W/WebVIEw﹕ java.lang.Throwable: A WebVIEw method was called on thread 'JavaBrIDge'. All WebVIEw methods must be called on the same thread. (Expected Looper Looper (main, tID 1) {2e799371} called on Looper (JavaBrIDge, tID 210) {72d6c48}, FYI main Looper is Looper (main, tID 1) {2e799371})        at androID.webkit.WebVIEw.checkThread(WebVIEw.java:2194)        at androID.webkit.WebVIEw.clearCache(WebVIEw.java:1451)        at com.mt.nad.testwebapp.MainActivity$JavaScriptInterface.gotoSite(MainActivity.java:56)        at com.androID.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)        at com.androID.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:28)        at androID.os.Handler.dispatchMessage(Handler.java:102)        at androID.os.Looper.loop(Looper.java:135)        at androID.os.HandlerThread.run(HandlerThread.java:61)12-29 03:54:04.100    2793-2838/com.mt.nad.testwebapp W/System.err﹕ java.lang.RuntimeException: java.lang.Throwable: A WebVIEw method was called on thread 'JavaBrIDge'. All WebVIEw methods must be called on the same thread. (Expected Looper Looper (main, tID 1) {2e799371} called on Looper (JavaBrIDge, tID 210) {72d6c48}, FYI main Looper is Looper (main, tID 1) {2e799371})12-29 03:54:04.100    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at androID.webkit.WebVIEw.checkThread(WebVIEw.java:2204)12-29 03:54:04.100    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at androID.webkit.WebVIEw.clearCache(WebVIEw.java:1451)12-29 03:54:04.101    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at com.mt.nad.testwebapp.MainActivity$JavaScriptInterface.gotoSit (MainActivity.java:56)12-29 03:54:04.101    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at com.androID.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)12-29 03:54:04.101    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at com.androID.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:28)12-29 03:54:04.101    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at androID.os.Handler.dispatchMessage(Handler.java:102)12-29 03:54:04.101    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at androID.os.Looper.loop(Looper.java:135)12-29 03:54:04.101    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at androID.os.HandlerThread.run(HandlerThread.java:61)12-29 03:54:04.101    2793-2838/com.mt.nad.testwebapp W/System.err﹕ Caused by: java.lang.Throwable: A WebVIEw method was called on thread 'JavaBrIDge'. All WebVIEw methods must be called on the same thread. (Expected Looper Looper (main, tID 1) {2e799371} called on Looper (JavaBrIDge, tID 210) {72d6c48}, FYI main Looper is Looper (main, tID 1) {2e799371})12-29 03:54:04.102    2793-2838/com.mt.nad.testwebapp W/System.err﹕ at androID.webkit.WebVIEw.checkThread(WebVIEw.java:2194)12-29 03:54:04.102    2793-2838/com.mt.nad.testwebapp W/System.err﹕ ... 7 more12-29 03:54:04.127    2793-2793/com.mt.nad.testwebapp I/chromium﹕ [INFO:CONSolE(19)] "Uncaught Error: Java exception was raised during method invocation", source: http://10.0.2.2/test-java-adapter/ (19)12-29 03:54:04.159    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000b4412-29 03:54:04.175    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000bd012-29 03:54:04.274    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ **** ERROR unkNown type 0x0 (glSizeof,72)12-29 03:54:04.299    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000b4412-29 03:54:04.323    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000bd012-29 03:54:04.448    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ **** ERROR unkNown type 0x0 (glSizeof,72)12-29 03:54:04.468    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000b4412-29 03:54:04.499    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000bd012-29 03:54:04.527    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ **** ERROR unkNown type 0x0 (glSizeof,72)12-29 03:54:04.537    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000b4412-29 03:54:04.549    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glutilsParamSize: unkNow param 0x00000bd012-29 03:54:04.580    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ **** ERROR unkNown type 0x0 (glSizeof,72)

资源:

http://10.0.2.2/test-java-adapter/ (19)

是指

<input type="button" value="Go To Site" onClick="window.CallJavaAdapter.gotoSite()">

如果使用Toast或TextVIEw,在单击按钮时可以更改,但对于WebVIEw loadurl(),它将不会加载…

我指的是:Android App: How to loadUrl in WebView from another class?但仍然没有运气……

解决方法:

更改

@JavaScriptInterfacepublic voID gotoSite() {    //Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();    webC.clearCache(true);//Here you call the methond in other thread    webC.loadUrl("http://Google.com");}

至:

@JavaScriptInterfacepublic voID gotoSite() {    //Toast.makeText(mContext, url, Toast.LENGTH_SHORT).show();    new Handler().post(new Runnable() {        @OverrIDe        public voID run() {            webC.clearCache(true);//Here you call the methond in UI thread            webC.loadUrl("http://Google.com");        }    });}
总结

以上是内存溢出为你收集整理的java – WebView loadurl() – android app全部内容,希望文章能够帮你解决java – WebView loadurl() – android app所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110728.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存