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

arsenal白 2015-05-01 02:17:43
能运行 就是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>

运行结果:




...全文
620 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
arsenal白 2015-05-01
  • 打赏
  • 举报
回复
引用 2 楼 clj1017772778 的回复:
加载的url地址一定记得要加http://,webview.loadurl("www.baidu.com");像这样是不行的。
我已经弄好了 还是谢谢你的回复
arsenal白 2015-05-01
  • 打赏
  • 举报
回复
我加了http://的
clj_风清云淡 2015-05-01
  • 打赏
  • 举报
回复
加载的url地址一定记得要加http://,webview.loadurl("www.baidu.com");像这样是不行的。
arsenal白 2015-05-01
  • 打赏
  • 举报
回复
顶一下, 怎么没人来

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧