首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

子线程经过handler更新UI还是报错

2013-08-24 
子线程通过handler更新UI还是报错程序源代码。。package com.ComDong.lunchmenutablueinkisbimport java.io

子线程通过handler更新UI还是报错
程序源代码。。

package com.ComDong.lunchmenutablueinkisb;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {
private TextView tvshowget;
private Button btnget;
private Handler handler;
private String HtmlSourceCode="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tvshowget = (TextView) findViewById(R.id.tvshowget);
        btnget=(Button) findViewById(R.id.btnget);
        handler=new Handler(){
        public void handleMessage(Message msg){
        tvshowget.setText(HtmlSourceCode);
        super.handleMessage(msg);
        }
        };
        
        btnget.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
           new Thread(new Runnable(){
                public void run(){
                getHtmlSourceCodeM2("http://www.baidu.com/");
                Message msg=handler.obtainMessage();


                handler.handleMessage(msg);
                }
                }).start();
           
        /*   for(int i=0;i<5;i++){
           if(HtmlSourceCode!="")
           tvshowget.setText(HtmlSourceCode);
           else
           handler.postDelayed(this,1000);*/
        }
        });
        
        

    }
    
    public void getHtmlSourceCode(String path) throws Exception{
    URL url=new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream in = conn.getInputStream();
byte[] data = readInputStream(in);
String html = new String(data);
tvshowget.setText(html);
    }
    
    public byte[] readInputStream(InputStream in) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
in.close();
return outStream.toByteArray();
    }
    
    public void getHtmlSourceCodeM2(String path){
    HttpClient httpclient=new DefaultHttpClient();
    HttpGet httpRequest=new HttpGet(path);
    HttpResponse httpResponse;
    try{
    httpResponse=httpclient.execute(httpRequest);
    if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
    HtmlSourceCode=EntityUtils.toString(httpResponse.getEntity());


    }else{
    Toast.makeText(MainActivity.this, "Failed to connect to the Internet", 1);
    }
    } catch(ClientProtocolException e){
    e.printStackTrace();
    } catch(IOException e){
    e.printStackTrace();
    }
    }
}



xml代码。。。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Get" />
    
    <TextView
        android:id="@+id/tv_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn_get"
        android:text="Default" />

</RelativeLayout>


permission有Internet

错误信息:
08-17 23:02:07.583: W/dalvikvm(16996): threadid=11: thread exiting with uncaught exception (group=0x41f72930)
08-17 23:02:07.583: E/AndroidRuntime(16996): FATAL EXCEPTION: Thread-1077
08-17 23:02:07.583: E/AndroidRuntime(16996): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.


08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.checkForRelayout(TextView.java:6452)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3696)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3554)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3529)
08-17 23:02:07.583: E/AndroidRuntime(16996): at com.ComDong.lunchmenutablueinkisb.MainActivity$1.handleMessage(MainActivity.java:35)
08-17 23:02:07.583: E/AndroidRuntime(16996): at com.ComDong.lunchmenutablueinkisb.MainActivity$2$1.run(MainActivity.java:46)
08-17 23:02:07.583: E/AndroidRuntime(16996): at java.lang.Thread.run(Thread.java:856)
08-17 23:02:07.653: D/dalvikvm(16996): GC_CONCURRENT freed 153K, 9% free 3840K/4184K, paused 2ms+1ms, total 18ms

谢谢 多线程 Android UI
[解决办法]
 getHtmlSourceCodeM2("http://www.baidu.com/");
                            Message msg=handler.obtainMessage();


                            handler.handleMessage(msg);
这里你handleMessage?应该是sendMessage吧,另外你的xml里控件的id和你java文件中的id都对不上,真不知道你怎么跑起来的

热点排行