【新手问题】Android浏览器中WebView加载不出网页

Android 码拜 9年前 (2015-05-10) 1939次浏览 0个评论
 

能运行 就是WebView控件显示不出网页来

【新手问题】Android浏览器中WebView加载不出网页
代码:
1.MainActivity.java
package com.example.simplebrowser;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

2.SimpleBrowser.java

package com.example.simplebrowser;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class SimpleBrowser extends Activity implements OnClickListener{

private EditText url;
private WebView ourBrow;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.simplebrowser);

ourBrow = (WebView)findViewById(R.id.wvBrowser);
ourBrow.loadUrl(“https://www.baidu.com”);

Button go = (Button)findViewById(R.id.bGo);
Button back = (Button)findViewById(R.id.bBack);
Button refresh = (Button)findViewById(R.id.bRefresh);
Button forward = (Button)findViewById(R.id.bForward);
Button clearHistory = (Button)findViewById(R.id.bHistory);
url = (EditText)findViewById(R.id.etURL);

go.setOnClickListener(this);
back.setOnClickListener(this);
refresh.setOnClickListener(this);
forward.setOnClickListener(this);
clearHistory.setOnClickListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bGo:
String theWebsite = url.getText().toString();
ourBrow.loadUrl(theWebsite);
break;
case R.id.bBack:
if(ourBrow.canGoBack())
ourBrow.goBack();
break;
case R.id.bForward:
if(ourBrow.canGoForward())
ourBrow.goForward();
break;
case R.id.bRefresh:
ourBrow.reload();
case R.id.bHistory:
ourBrow.clearHistory();
break;
}

}
}
3.simplebrowser.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >
    <LinearLayout 
        android:orientation=”horizontal”
        android:weightSum=”10″
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”>
        <EditText 
            android:layout_width=”match_parent”
            android:layout_weight=”2″
            android:layout_height=”wrap_content”
            android:id=”@+id/etURL”/>
        <Button 
            android:text=”转到”
            android:id=”@+id/bGo”
            android:layout_weight=”8″
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”/>
    </LinearLayout>
    <LinearLayout 
        android:orientation=”horizontal”
        android:weightSum=”8″
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”>
        <Button android:text=”返回”
            android:id=”@+id/bBack”
            android:layout_weight=”2″
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”/>
        <Button android:text=”向前”
            android:id=”@+id/bForward”
            android:layout_weight=”2″
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”/>
        <Button android:text=”刷新”
            android:id=”@+id/bRefresh”
            android:layout_weight=”2″
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”/>
        <Button android:text=”清除”
            android:id=”@+id/bHistory”
            android:layout_weight=”2″
            android:layout_width=”match_parent”
            android:layout_height=”wrap_content”/>
        
    </LinearLayout>

    <WebView 
        android:id=”@+id/wvBrowser”
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”/>”
</LinearLayout>

运行结果:
【新手问题】Android浏览器中WebView加载不出网页

顶一下,
怎么没人来
50分
加载的url地址一定记得要加http://,webview.loadurl(“www.baidu.com”);像这样是不行的。
我加了http://的
引用 2 楼 clj1017772778 的回复:

加载的url地址一定记得要加http://,webview.loadurl(“www.baidu.com”);像这样是不行的。

我已经弄好了 还是谢谢你的回复


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明【新手问题】Android浏览器中WebView加载不出网页
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!