android多线程 UI无法更新??请教
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flv01 = (ListView) findViewById(R.id.flv01);
flv02= (ListView) findViewById(R.id.flv02);
runable1 run1=new runable1();
runable1 run2=new runable1();
Thread thread1 = new Thread(run1);
Thread thread2 = new Thread(run1);
try {
thread1.start();
thread1.join();
thread2.start();
thread2.join();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}
public class runable1 implements Runnable{
@Override
public void run() {
System.out.println("本地OK!!");
dbUtil_1 = new dbUtil();
List<HashMap<String, String>> list3 = new ArrayList<HashMap<String, String>>();
list3.clear();
list3 = dbUtil_1.chakan(参数1);
System.out.println("0000000"+list3);
adapter = new SimpleAdapter(
xiancheng.this,
list3,
R.layout.lv02,
new String[]{"biaoti"},
new int[]{R.id.stv00});
System.out.println("赋值"+list3);
flv01.post(new Runnable(){
public void run(){
flv01.setAdapter(adapter);
}});}}
public class runable2 implements Runnable{
@Override
public void run() {
System.out.println("本地OK!!");
dbUtil_1 = new dbUtil();
List<HashMap<String, String>> list4 = new ArrayList<HashMap<String, String>>();
list4.clear();
list4 = dbUtil_1.chakan(参数2);
System.out.println("0000000"+list4);
adapter = new SimpleAdapter(
xiancheng.this,
list4,
R.layout.lv02,
new String[]{"biaoti"},
new int[]{R.id.stv00});
flv02.post(new Runnable(){
public void run(){
flv02.setAdapter(adapter);
}});}}}
结果到红色字体打印正常返回值;
然后就是:
Skipped 91 frames! The application may be doing too much work on its main thread.
组件无法获取值,请问是怎么回事儿?
多线程 android UI更新
[解决办法]
adapter = new SimpleAdapter(
xiancheng.this,
list3,
R.layout.lv02,
new String[]{"biaoti"},
new int[]{R.id.stv00});
System.out.println("赋值"+list3);
flv01.post(new Runnable(){
public void run(){
flv01.setAdapter(adapter);
这些不要放子线程里操作
[解决办法]
thread1.start();
thread1.join();
这是什么意思 为什么启动线程了之后 还要再join一次?
[解决办法]
flv02.setAdapter(adapter);线程里面不能有这样的ui操作,要handler出去
[解决办法]
使用Handler刷新界面啊
[解决办法]
1,主线程不能join子线程。开子线程的目的就是耗时的操作慢慢跑,主线程尽快返回。
2,子线程刷新UI用postInvalidate或者Handler。