Android网页视图没有作为屏幕的一部分显示



我的活动代码

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

但是web view没有显示

请看你们班的MyJavaScriptChromeClient。我用一个简单的WebChromeClientWebViewClient试过你的代码,它工作了。

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

最新更新