在子线程中更新ProgressBar为null

1   就是自己ProgressBar有两个,我在findViewbyId时填了另一个,所以tm的搞了半天才找到错误。

2 另外有个问题,先写在这,记得以前遇到过,忘了咋解决的了

这里在另一个子线程中修改pb_antivirus,这两个线程的步调可能不一致的啊,但是为啥能够正常执行?有大神解决吗

private void initUI() {
        iv_antivirus_bg=(ImageView)findViewById(R.id.iv_antivirus_bg);
        iv_antivirus_bg1=(ImageView)findViewById(R.id.iv_antivirus_bg1);
        tv_antivirus=(TextView)findViewById(R.id.tv_antivirus);
        pb_antivirus=(ProgressBar)findViewById(R.id.pb_antivirus);
        ll_layout=(LinearLayout)findViewById(R.id.ll_layout);
    }

private
void initData() { new Thread(){ public void run() { mAntivirusList=AntivirusDao.getAntivirus(); PackageManager pm=getPackageManager(); List<PackageInfo> packageInfoList=pm.getInstalledPackages( PackageManager.GET_SIGNATURES+PackageManager.GET_UNINSTALLED_PACKAGES); mAntivirusListT=new ArrayList<Antivirus>(); pb_antivirus.setMax(packageInfoList.size()); for(PackageInfo pif:packageInfoList){ Antivirus antivirus=new Antivirus(); //病毒对象个体 Signature[] signatures=pif.signatures; Signature signature=signatures[0]; String string=signature.toCharsString(); String md5=MD5Util.encoder(string); for(Antivirus a:mAntivirusList){ if(a.getMd5().equals(md5)){ antivirus.setAntivirus(true); antivirus.setDesc(a.getDesc()); mAntivirusListT.add(antivirus); }else{ antivirus.setAntivirus(false); } } String name=pif.applicationInfo.loadLabel(pm).toString(); antivirus.setName(name); try { Thread.sleep(50+new Random().nextInt(100)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } progress++; pb_antivirus.setProgress(progress); Message msg=Message.obtain(); msg.obj=antivirus; msg.what=SCANING; mHandler.sendMessage(msg); } Message msg=Message.obtain(); msg.what=SCAN_FINISH; mHandler.sendMessage(msg); }; }.start(); }
原文地址:https://www.cnblogs.com/zzl521/p/8849386.html