2014-12-29 142 views
3

我點擊webview頁面內的按鈕時出錯。據說,當我點擊按鈕,它會改變爲谷歌網站。下面是代碼和錯誤: -WebView loadurl() - android應用程序

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()"> 

如果使用吐司或TextView的,在點擊的按鈕,但對於web視圖使用loadURL時可以改變(),它不會加載...

我指的是:Android App: How to loadUrl in WebView from another class?但仍沒有運氣...

+2

爲什麼白癡的人downvote正常嗎?沒有任何解釋 – user2162550

回答

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

} 
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"); 
      } 
     }); 
    } 
+0

它會很有用,如果你添加一些描述/評論到你的代碼 – prasun