WebView loadurl() - Android应用程序

时间:2014-12-29 04:51:32

标签: java android webview

当我点击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;

@Override
protected 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");
    }
}

@Override
public 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;
}

@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();

    //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 more
12-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 0x00000b44
12-29 03:54:04.175    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
12-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 0x00000b44
12-29 03:54:04.323    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
12-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 0x00000b44
12-29 03:54:04.499    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
12-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 0x00000b44
12-29 03:54:04.549    2793-2832/com.mt.nad.testwebapp E/eglCodecCommon﹕ glUtilsParamSize: unknow param 0x00000bd0
12-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?但仍然没有运气......

2 个答案:

答案 0 :(得分:3)

更改

@JavascriptInterface
public 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");
}

为:

@JavascriptInterface
public 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");
        }
    });

}

答案 1 :(得分:3)

private void resetWebUrl(final String url){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView.clearCache(true);//Here you call the methond in UI thread
                webView.loadUrl("www.google.com");
            }
        });
    }