android程序运行后无法加载UI

程序运行后无法加载UI,出现白屏,并报错如下:

07-18 01:30:20.459: E/WindowManager(674): Starting window AppWindowToken{42322000 token=Token{4241ffd8 ActivityRecord{41eb6d98 u0 com.example.pls/.MainActivity t30}}} timed out

在我将主线程中的如下语句注释后,便可以正常加载UI。
图片说明

MyThread类如下(主要是考虑单线程模型,想另开一个线程做任务)。

     class MyThread implements Runnable {

        private byte[] src = new byte[1024];
        private byte[] dst = new byte[1024];

        public void run() { 

            while (!Thread.currentThread().isInterrupted()) {

                 try {
                    int fd = portOperation.getFd();
                    //循环前清空
                    String sResponse = "100003010300000712";
                    byte[] bResponse = HexString2Bytes(sResponse);
                    portOperation.WriteSerialByte(fd, bResponse);

                    String sCardInfo = "1000030101000512";
                    byte[] bCardInfo = HexString2Bytes(sCardInfo);//[16, 0, 1, 1, 1, 0, 3, 18]
                    portOperation.WriteSerialByte(portOperation.getFd(),bCardInfo);

                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){        
                        e.printStackTrace();
                    }

                    src = portOperation.ReadSerial(fd,1024 );

                    if ((src != null) && (src.length > 9)) {
                        String debug = bytesToHexString(src);  //调试

                        //转码恢复
                        dst = recovery(src);

                        //从返回帧中提取卡数
                        String sTemp = bytesToHexString(dst);
                        int a = Integer.parseInt(String.valueOf(sTemp.charAt(11)));
                        int b = Integer.valueOf(String.valueOf(sTemp.charAt(12)),16);
                        int cardNum = a*16+b;


                        for(int i = 0;i<cardNum;i++){
                            int j = i*16;
                            String cardID = sTemp.substring(13+j,21+j );

                            if (!dbFile.exists()) {
                                DisplayToast("请先将数据库拷贝到SD卡根目录下!");
                                return;
                            }

                            String SQL = String.format("select * from employInfo where cardID = '%s'", cardID);
                            Cursor c = db.rawQuery(SQL, null);

                            //没有读到马丽但读到其他卡时,在此停止(因为数据库中只有马丽),不会执行超时删除
                            //而若读到马丽时,只会更新马丽时间

                            if(c.moveToFirst()){
                                //Cursor默认从下标为-1的地方开始,所以不能马上从Cursor获取信息
                                String name = c.getString(c.getColumnIndex("name"));

                                //在此处将读取人员姓名保存至“employinfo.txt”文件中(确保不重复)
                                Date date = new Date(localFile.lastModified());//文件最后修改时间
                                Calendar calendar = Calendar.getInstance();
                                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                try{
                                    //保证文件日期为今日
                                    if(!localFile.exists()){
                                        localFile.createNewFile();
                                    }else if(localFile.exists() && date.getDay() != calendar.get(Calendar.DAY_OF_MONTH) ){
                                        localFile.createNewFile();
                                    }

                                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(localFile,true));
                                    BufferedReader bufferedReader = new BufferedReader(new FileReader(localFile));

                                    if(!hasName(name, bufferedReader)){

                                        bufferedWriter.write(name+"\t"+dateFormat.format(calendar.getTime())+"\n");
                                        bufferedWriter.flush(); 
                                    }

                                    bufferedReader.close(); 
                                    bufferedWriter.close();

                                }catch(IOException e){

                                    e.printStackTrace();
                                    Log.e("txt",e.toString());
                                }


                                c.close();
                                String photoPath0 = Environment.getExternalStorageDirectory().toString() + "/picSrc/";
                                String photoPath = String.format("%s%s.bmp",photoPath0,name);

                                //确保list里面放的是不同对象map的堆地址,指向的是不同对象
                                Map<String, Object> map = new HashMap<String, Object>();

                                map.put("itemName", name);
                                //注意此处不可将Bitmap对象直接放入hashmap中,而应传入地址
                                map.put("itemPhoto", photoPath);

                                map.put("addTime",System.currentTimeMillis());
                                Log.v("map", map.toString());

                                //根据姓名查看list当中是否存在所有项
                                boolean exist = false;
                                for(Map<String, Object> tempMap : list){
                                    if(name.equals(tempMap.get("itemName"))){
                                        exist = true;
                                        //如果有则更新时间
                                        tempMap.put("addTime", System.currentTimeMillis());
                                        break;
                                    }
                                }
                                //如果不存在,则添加到list当中
                                if(!exist){
                                    list.add(map);
                                }


                                Log.v("removeb2",list.toString());
                                //遍历list查看有无超时项,有则清空该项
                                Iterator<Map<String, Object>> it = list.iterator();
                                while(it.hasNext()){
                                    if((System.currentTimeMillis()-Long.parseLong(it.next().get("addTime").toString()))>60000){
                                        Log.v("removebefore",list.toString());
                                        it.remove();
                                        Log.v("removeafter",list.toString());
                                    }
                                }
                                /*for(int k=0;k<list.size();k++){

                                    if(System.currentTimeMillis()-Long.parseLong(list.get(k).get("addTime").toString())>30000){
                                        long debug1 = System.currentTimeMillis();
                                        long debug2 = Long.parseLong(list.get(k).get("addTime").toString());
                                        Log.v("removebefore",list.toString());
                                        list.remove(k);
                                        Log.v("remove",list.remove(k).toString());
                                        Log.v("removeafter",list.toString());
                                    }
                                }*/

                                /*//删除list中没有但listview中
                                for(int m = 0;m<lv.getChildCount();m++){
                                    View viewTemp = lv.getChildAt(m);
                                    String nameTemp = viewTemp.findViewById(R.id.itemName).toString();

                                    boolean exist1 = false;
                                    for(Map<String, Object> tempMap : list){
                                        if(nameTemp.equals(tempMap.get("itemName"))){
                                            exist1 = true;
                                            break;
                                        }
                                    }
                                    if(!exist1){
                                        lv.removeView(viewTemp);
                                    }
                                }*/
                            }   
                        }  


                        //将定时任务中获取的List装入ListView中

                        Message msg = new Message();  
                        msg.what = 1;  
                        handler.sendMessage(msg);  

                    }else{

                        Message msg = new Message();  
                        msg.what = 2;  
                        handler.sendMessage(msg);  
                    }   

                 }catch(UnsupportedEncodingException e){
                     e.printStackTrace();   
                 }

                 try {   
                     Thread.sleep(500);    
                } catch (InterruptedException e) {   
                     Thread.currentThread().interrupt();   
                }   

            }
        }
    }

不知道具体是什么原因,还请各位前辈赐教,谢谢!

2个回答

Thread类中run()和start()方法的区别如下:

    run()方法:在本线程内调用该Runnable对象的run()方法,可以重复多次调用;

    start()方法:启动一个线程,调用该Runnable对象的run()方法,不能多次启动一个线程;
a739924135
净坛腐儒 干净利落,切中要害,谢谢前辈!
3 年多之前 回复

线程的开启是调用start方法。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问