2013-04-17 2 views
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())); 
}} 

내가 로그 캣을 확인

<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> 

내 XML 코드, showHtml() 메소드가 호출되는 (그 인쇄의 일부로 표시 안한다 "in html").

하지만 웹보기가 표시되지 않습니다.

답변

1

클래스 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; 
     } 
    });