在写一个小应用的时候,我接触到了一个关于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段)。
好了,完了,就这么多,希望对大家有帮助