1、如下图所言,必须在UI线程中调用异步任务的子类
2、经过碰壁 测试
1--、方法一中 @Override public void onClick(DialogInterface dialog, int which) {Thread thread=new Thread(new Runnable() { @Override public void run() { uploaddd(); } }); thread.start(); 2--、方法二中 private void uploaddd() { final Handler handler=new Handler() { public void handleMessage(Message msg) { if(msg.what==1) { Log.d("DFDFDFDF",ii+"__"+"haode"); } } }; Thread thread =new Thread(new Runnable() { @Override public void run() { Log.d("DFDFDFDF",ii+"__"); handler.sendEmptyMessage(1); } }); thread.start(); }
上述代码会挂掉 证明得出 在非UI中 调用的方法继续开启线程同时有Handler机制会出现异常(因为Handler是异步机制)
实验得出下述代码不会出现异常
1-- @Override public void onClick(DialogInterface dialog, int which) {Thread thread=new Thread(new Runnable() { @Override public void run() { uploaddd(); } }); thread.start(); 2-- private void uploaddd() { // final Handler handler=new Handler() // { // public void handleMessage(Message msg) // { // if(msg.what==1) // { // Log.d("DFDFDFDF",ii+"__"+"haode"); //// if(AndroidClient._error_uploadScrapImgFile.intern()!="") //// { //// Toast.makeText(ActivityScrap.this,AndroidClient._error_uploadScrapImgFile,Toast.LENGTH_SHORT).show(); //// } // } // } // }; Thread thread =new Thread(new Runnable() { @Override public void run() { Log.d("DFDFDFDF",ii+"__"); AndroidClient.uploadScrapImgFile(scrapImgDatas,ii+"__"); //handler.sendEmptyMessage(1); } }); thread.start(); }
时间: 2024-11-10 13:57:19