android webview 無法顯示

mapleandkw 发布于 2012/05/16 16:48
阅读 2K+
收藏 0

在學習webview的時候,html內容無法顯示,且也沒有錯誤提示

請各位大俠幫忙看看問題出在哪裡
package com.iaiai.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.iaiai.activity.WebViewActivity.Person;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

/**
 * @author ricky
 *2012/5/16 下午12:00:52
 */
public class WebViewActivity extends Activity {

 private WebView webView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.webview);
  webView=(WebView)findViewById(R.id.webView);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new PersonService(), "personService");
  webView.loadUrl("file:///android_asset/test.html");
  webView.setWebChromeClient(new WebChromeClient(){

   @Override
   public void onProgressChanged(WebView view, int newProgress) {
    // TODO Auto-generated method stub
    setTitle("頁面加載中....."+newProgress+"%");
    super.onProgressChanged(view, newProgress);
   }
   
  });
 }
 
 

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  // TODO Auto-generated method stub
  if(webView.canGoBack()&&keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
   webView.goBack();
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }

 

 class PersonService{
  public void getPersonList(){
   List<Person> list=getPersonDao();
   JSONArray array=new JSONArray();
   for(Person p:list){ 
    try {
    JSONObject obj=new JSONObject();
    obj.put("name",p.getName());
    obj.put("age", p.getAge());
    obj.put("phone", p.getPhone());
    array.put(obj);
    } catch (JSONException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
   }
   webView.loadUrl("javascript:show('"+array.toString()+"')");
  }
   //打电话的方法    
         public void call(String mobile){   
             Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile));   
             startActivity(intent);   
         }   


  private List<Person> getPersonDao(){
   List<Person> list =new ArrayList<Person>();   
   list.add(new Person("aa",32,"男", "13675574545"));   
   list.add(new Person("bb",33, "女","13698874545"));   
         list.add(new Person("cc",34, "男","13644464545"));   
         list.add(new Person("dd",35,"男" ,"13908978877"));   
         list.add(new Person("ee",36,"女","15908989898")); 
         return list;
  }
 }
 
 class Person{
  private String name;
  private int age;
  private String sex;
  private String phone;
  public Person(String name,int age,String sex,String phone){
   this.name=name;
   this.age=age;
   this.sex=sex;
   this.phone=phone;
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public int getAge() {
   return age;
  }
  public void setAge(int age) {
   this.age = age;
  }
  public String getSex() {
   return sex;
  }
  public void setSex(String sex) {
   this.sex = sex;
  }
  public String getPhone() {
   return phone;
  }
  public void setPhone(String phone) {
   this.phone = phone;
  }
  
 }
}

 

test.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
 <html>   
 <head>   
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
 <title>Insert title here</title>   
 <script type="text/javascript">   
     function show(jsondata){   
             var jsonobjs = eval(jsondata);   
             var table = document.getElementById("personTable");   
             for(var y=0; y<jsonobjs.length; y++){   
                 var tr = table.insertRow(table.rows.length);
                 var td1 = tr.insertCell(0);   
                 var td2 = tr.insertCell(1);    
                 var td3 = tr.insertCell(2);     
                 td1.innerHTML = jsonobjs[y].name;    
                 td2.innerHTML = jsonobjs[y].age;    
                 td3.innerHTML = "<a href='javascript:personService.call(\""+ jsonobjs[y].phone+ "\")'>"+ jsonobjs[y].phone+ "</a>";    
             }   
     }   
 </script>   
 </head>   
 <body onload="javascript:personService.getPersonList()">   
    <table border="0" width="100%" id="personTable" cellspacing="0">   
         <tr>   
             <td width="40%" align="center">姓名</td><td width="40%">年齡</td><td align="center">電話</td>   
         </tr>   
     </table>   
     <a href="javascript:window.location.reload()">刷新</a>   
</body>   
 </html>

 


 

加载中
0
蓄丰
蓄丰

路过,学习了。我试了没有问题。

谢谢!

返回顶部
顶部