我在从网址下载html文件时遇到问题。似乎html文件应该存储在代码中,但webview不会显示它。我一直在接受 nativeOnDraw失败;清除logcat中的背景颜色消息。
public class MainActivity extends Activity {
WebView display;
Button goButton;
//Message msg;
EditText urlEditText;
Handler showContent = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
display.loadData((String) msg.obj, "text/html", "UTF-8");
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (WebView) findViewById(R.id.display);
urlEditText = (EditText) findViewById(R.id.urlText);
goButton = (Button) findViewById(R.id.go);
goButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(){
@Override
public void run(){
URL url2 = null;
try {
//url2 = new URL(urlEditText.getText().toString());
url2 = new URL("http://www.google.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(url2.openStream()));
String reply = " ", tempReply = " ";
tempReply = reader.readLine();
while (tempReply != null){
reply = reply + tempReply;
tempReply = reader.readLine();
}
Message msg = Message.obtain();
msg.obj = reply;
showContent.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
}
});
}