0

我的活动代码

public class MainActivity extends Activity {
Spinner spinnerProduct;
WebView webView1, webView2, webView3;
private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spinnerProduct = (Spinner) findViewById(R.id.spinner_select_product);

    List<String> list = new ArrayList<String>();
    list.add("Product 1");
    list.add("Product 2");
    list.add("Product 3");

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
    spinnerProduct.setAdapter(dataAdapter);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerProduct.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Toast.makeText(getApplicationContext(), "Product " + ++arg2 + " selected", Toast.LENGTH_LONG).show();
            showHtml();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
}

public void showHtml() {
    Log.v("in showHtml", "in showHtml");
    webView1 = (WebView) findViewById(R.id.web_view1);
    WebSettings webSettings = webView1.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webSettings.setSavePassword(true);
    webSettings.setSaveFormData(true);
    webSettings.setSupportZoom(true);

    // webView1.addJavascriptInterface(new DemoJavaScriptInterface(),
    // "demo");

    webView1.loadUrl("file:///android_asset/product.html");

    webView1.setWebChromeClient(new MyJavaScriptChromeClient(getApplicationContext()));
}}

我的xml代码

<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="10"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/ll_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <TextView
        android:id="@+id/tv_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/app_name" />
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_select_product"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:gravity="center"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner_select_product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_web_views"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="7"
    android:orientation="horizontal"
    android:weightSum="3" >

    <WebView
        android:id="@+id/web_view1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <WebView
        android:id="@+id/web_view2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <WebView
        android:id="@+id/web_view3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

我检查了 logcat,调用了 showHtml() 方法(它打印“in html”)。

但网页视图不会显示。

4

1 回答 1

1

Please have a look at your class MyJavaScriptChromeClient. I've tried your code with a simple WebChromeClient and WebViewClient and it worked.

webView1.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress)
        {

        }
    });
    webView1.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });
于 2013-04-17T14:02:10.523 回答