代码很简单请查看:package com.android.messageexample;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MyActivity extends Activity{ Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mButton=new Button(this); mButton.setText("Button"); setContentView(mButton); mButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { TextThread noLooerThread = new TextThread(); //Only the original thread that created a view hierarchy can touch its views. //这里将报错 提示不是UI主线程无法更改UI noLooerThread.setName("fail"); noLooerThread.start(); } }); TextThread noLooerThread = new TextThread(); noLooerThread.setName("success"); noLooerThread.start(); } public class TextThread extends Thread { public void run() { if("success".equals(this.getName())){ mButton.setText("success"); else if("fail".equals(this.getName())){ mButton.setText("fail"); } } }}
第一个线程在Oncread里面运行 成功
第二个线程在onclick()里面运行 报错提示是因为它不是原来创建UI的线程 我怎么知道他是不是原来创建UI的线程?
还有onclick里面可以改变Button的外观 (可能会出现onclick也只是一个线程而已 然而在线程里面开启线程然后再改变就不能改变)
有心人就可能会测试在onCreate里面的那个线程里面再创建一个线程 尝试改变UI列:
package com.android.messageexample;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MyActivity extends Activity{ Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mButton=new Button(this); mButton.setText("Button"); setContentView(mButton); mButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { TextThread noLooerThread = new TextThread(); //Only the original thread that created a view hierarchy can touch its views. //这里将报错 提示不是UI主线程无法更改UI noLooerThread.setName("fail"); noLooerThread.start(); } }); TextThread noLooerThread = new TextThread(); noLooerThread.setName("success"); noLooerThread.start(); } public class TextThread extends Thread { public void run() { if("success".equals(this.getName())){ TextThread noLooerThread = new TextThread(); noLooerThread.setName("success1"); //这里也会成功 noLooerThread.start(); } else if("fail".equals(this.getName())){ mButton.setText("fail"); }else if("success1".equals(this.getName())){ mButton.setText("success1"); } } }}
谁能告诉我为什么吗?
第一个线程在Oncread里面运行 成功
第二个线程在onclick()里面运行 报错提示是因为它不是原来创建UI的线程 我怎么知道他是不是原来创建UI的线程?
还有onclick里面可以改变Button的外观 (可能会出现onclick也只是一个线程而已 然而在线程里面开启线程然后再改变就不能改变)
有心人就可能会测试在onCreate里面的那个线程里面再创建一个线程 尝试改变UI列:
package com.android.messageexample;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MyActivity extends Activity{ Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mButton=new Button(this); mButton.setText("Button"); setContentView(mButton); mButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { TextThread noLooerThread = new TextThread(); //Only the original thread that created a view hierarchy can touch its views. //这里将报错 提示不是UI主线程无法更改UI noLooerThread.setName("fail"); noLooerThread.start(); } }); TextThread noLooerThread = new TextThread(); noLooerThread.setName("success"); noLooerThread.start(); } public class TextThread extends Thread { public void run() { if("success".equals(this.getName())){ TextThread noLooerThread = new TextThread(); noLooerThread.setName("success1"); //这里也会成功 noLooerThread.start(); } else if("fail".equals(this.getName())){ mButton.setText("fail"); }else if("success1".equals(this.getName())){ mButton.setText("success1"); } } }}
谁能告诉我为什么吗?
为什么在onclick 事件里面启动一个线程则不可更改了?
但是在onclick里面却可以更改
如果想在onClick中通过线程操作Activity的View, 可以先让MyActivity实现OnClickListener接口(implements OnClickListener),然后定义属于Activity的onClick方法,最后通过mButton.setOnClickListener(this)设置监听器。
android.os.AsyncTask<Params, Progress, Result>
这个类就可以单独开线程
不过我现在发现利用这个的一个问题,重复几次以后,android.os.AsyncTask<Params, Progress, Result>就没有开启新线程的效果,应该和activity在同一线程了,怀疑是AsyncTask堆栈需要管理,很怀疑有最大值之类的,正在研究,谁知道告诉我,万分感谢(发我空间留言就可以,谢谢)