默认的webview无效

时间:2012-10-03 08:02:22

标签: android android-webview

您好我正在使用listview onclick加载webview这很好。

我的问题是在onclick listview之前,我使用默认网页加载到webview中 这不起作用......

这是我的代码

webview=(WebView)app.findViewById(R.id.webView);
webview.loadUrl("http://google.com");//this is my default webview before onclick the list view

以上默认网址未加载...

ListView listView = (ListView) menu.findViewById(R.id.list);
initListView(listView);
public  void initListView(final ListView listView) {
    // By using setAdpater method in listview we an add string array in list.
    ArrayList<ItemDetails> image_details = GetSearchResults();
    listView.setAdapter(new ItemListBaseAdapter(this, image_details));

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
            Object o = listView.getItemAtPosition(position);
            ItemDetails obj_itemDetails = (ItemDetails)o;
            String msg =obj_itemDetails.getName();
              try{
            if(null!=webview){
                webview.removeAllViews();
              }
            if(msg.equalsIgnoreCase("a")){
                 webview.loadUrl("http://example.com");
            }else if(msg.equalsIgnoreCase("b")){
                 webview.loadUrl("http://example.com");
            }else if(msg.equalsIgnoreCase("c")){
                 webview.loadUrl("http://example.com");
            }else if(msg.equalsIgnoreCase("d")){
                 webview.loadUrl("http://e.com");
            }else if(msg.equalsIgnoreCase("e")){
                 webview.loadUrl("http://f.com");
            }else{
                 webview.loadUrl("http://example.com");
            }


          }catch (Exception e) {
            Toast.makeText(HorzScrollWithListMenu.this,e.toString(),Toast.LENGTH_LONG).show();
            e.printStackTrace(System.out);

            }

        }  
    });

}

我的onCreate代码部分

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflater = LayoutInflater.from(this);
    scrollView = (MyHorizontalScrollView)   inflater.inflate(R.layout.horz_scroll_with_list_menu, null);
    setContentView(scrollView);

    menu = inflater.inflate(R.layout.horz_scroll_menu, null);
    app = inflater.inflate(R.layout.horz_scroll_app, null);
    webview=(WebView)app.findViewById(R.id.webView);
    webview.loadUrl("http://google.com");

请告诉我代码中出了什么问题。?

2 个答案:

答案 0 :(得分:0)

我认为这会对你有帮助

webView = (WebView) app.findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com")

也不要忘记在清单文件中添加此权限,

 <uses-permission android:name="android.permission.INTERNET" />

或者使用 WebViewClient

web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.google.com");


 public class myWebClient extends WebViewClient
    {
     @Override
     public void onPageStarted(WebView view, String url, Bitmap favicon) {
      // TODO Auto-generated method stub
      super.onPageStarted(view, url, favicon);
     }

     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
      // TODO Auto-generated method stub

      view.loadUrl(url);
      return true;

     }

     @Override
     public void onPageFinished(WebView view, String url) {
      // TODO Auto-generated method stub
      super.onPageFinished(view, url);

      progressBar.setVisibility(View.GONE);
     }
    }

答案 1 :(得分:0)

尝试在webView上设置这些属性

webview.clearCache(true);
webview.getSettings().setJavaScriptEnabled(true);

尝试使用本教程以正确理解Web View的工作原理 Here