android中Handler、Thread、Runnable更新UI的思考

在写一个小应用的时候,我接触到了一个关于Handler和Thread以及runnable的问题,问了群里各种人都解决不了,然后查阅大量资料,终于有些了解,现在发上来和大家分享。

主要是关于,Thread和Runnable的区别,以及他们在使用Handler时造成的影响~,我先上代码

下面这个是错误的代码~下面的代码只要执行到handler中更新UI的部分就会报空指针错误~线程处用的Thread

public class MainActivity extends Activity {
	TextView text1, text2;
	Button button;
	myThread th;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text1 = (TextView)findViewById(R.id.text1);
		text2 = (TextView)findViewById(R.id.text2);
		button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				th = new myThread();
				th.start();
			}
		});
	}
	Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				System.out.println("-----------1--------------");
				System.out.println(msg.getData().getString("s1"));
				System.out.println(msg.getData().getString("s2"));
				try{
				 text1.setText(msg.getData().getString("s1"));
				 text2.setText(msg.getData().getString("s1"));
				}catch(Exception e){
					e.printStackTrace();
				}
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
}

class myThread extends Thread{
	String s1, s2;
	MainActivity ma = new MainActivity();
	public void run(){
		  s1 = "fsdfsgfdsgdfgfdgdhshshs";
		  s2 = "fsfsdgdshdhdshrehreherh";

		  
		  Message msg = new Message();
		  msg.what = 1;
		  Bundle bundle = new Bundle();
		  bundle.putString("s1", s1);
		  bundle.putString("s2", s2);
		  msg.setData(bundle);
		  ma.myHandler.sendMessage(msg);
	}
}

上面这段代码,没给注释,但是很简单,相信大家都能看懂~,这段代码是错误的。

下面给大家上正确的代码~线程处用的Runnable

public class MainActivity extends Activity {
	TextView text1, text2;
	Button button;
	Thread th;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text1 = (TextView)findViewById(R.id.text1);
		text2 = (TextView)findViewById(R.id.text2);
		button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				th = new Thread(runnable);
				th.start();
			}
		});
	}
	Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				System.out.println("-----------1--------------");
				System.out.println(msg.getData().getString("s1"));
				System.out.println(msg.getData().getString("s2"));
				try{
				 text1.setText(msg.getData().getString("s1"));
				 text2.setText(msg.getData().getString("s1"));
				}catch(Exception e){
					e.printStackTrace();
				}
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
		      String s1 = "fsdfsgfdsgdfgfdgdhshshs";
			  String s2 = "fsfsdgdshdhdshrehreherh";

			  Message msg = new Message();
			  msg.what = 1;
			  Bundle bundle = new Bundle();
			  bundle.putString("s1", s1);
			  bundle.putString("s2", s2);
			  msg.setData(bundle);
			  MainActivity.this.myHandler.sendMessage(msg);
		}
	};
}

这是正确的代码,能正确更新UI。


找到问题以后,我又做了几个小实验,发现,其实用Thread也是可以的,也是可以更新UI的,就像这样

public class MainActivity extends Activity {
	TextView text1, text2;
	Button button;
	myThread th;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text1 = (TextView)findViewById(R.id.text1);
		text2 = (TextView)findViewById(R.id.text2);
		button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				th = new myThread();
				th.start();
			}
		});
	}
	Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				System.out.println("-----------1--------------");
				System.out.println(msg.getData().getString("s1"));
				System.out.println(msg.getData().getString("s2"));
				try{
				 text1.setText(msg.getData().getString("s1"));
				 text2.setText(msg.getData().getString("s1"));
				}catch(Exception e){
					e.printStackTrace();
				}
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	class myThread extends Thread{
		public void run() {
			// TODO Auto-generated method stub
		      String s1 = "fsdfsgfdsgdfgfdgdhshshs";
			  String s2 = "fsfsdgdshdhdshrehreherh";

			  Message msg = new Message();
			  msg.what = 1;
			  Bundle bundle = new Bundle();
			  bundle.putString("s1", s1);
			  bundle.putString("s2", s2);
			  msg.setData(bundle);
			  MainActivity.this.myHandler.sendMessage(msg);
	}
	}
}


到这,不知道大家发现问题在哪没有?

据我所了解~Thread和Runnable的区别如下:

如果然一个线程实现Runnable接口,当调用这个线程对象开辟多线程时,可以让这些线程调用同一变量~,(说白了就是,我主线程和抛出的子线程都可以用你主线程的变量了)

若这个线程类是继承自Thread类而来,则通过内部类实现上述功能,利用的就是内部类可任意访问外部类这一特性(这段说的就是我3段代码的最后1段)。

好了,完了,就这么多,希望对大家有帮助



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值