问题描述
- android 当在主线程里开启一个新线程 程序就崩溃
- 我要实现的功能是检索SDcard里的所有txt文件
我尝试过很多办法1 不用线程 直接在主线程里检索文件 由于有循环且检索时间会超过5秒 导致黑屏然后提示未响应
2 我建立一个线程让线程去检索文件当检索完毕后把检索到的文件传递给主类 主类再把检索到的文件显示在list上 但是当线程一启动程序就崩溃了
3 最后我在主类里添加handler 当子线程检索完毕就sendmessage给主类 然后主类把子线程检索到的文件显示在list上但程序还是崩溃 我没在子线程里操作UI
好像只要子线程里有循环程序就会崩溃下面我把代码贴出来希望大家能帮帮我解决下这个问题
主类的主要代码
enter code here int jishuqi = 0;ProgressDialog progress;jiansuothread thread;Intent intent;int[] leixing_xuanzhe;FileReader file;File fuwenjian;File fuwenjianxia[];String[] leixing = new String[] {""doc""pdf""xls""txt""jpg""ppt""};int i = 0;ArrayList<File[]>root = new ArrayList<File[]>();String xuanxiang [] = new String [] {""向上""编辑""添加""删除""};int ico [] = new int[] {R.drawable.xinjianR.drawable.xinjianR.drawable.xinjianR.drawable.xinjian};ListView list;Button xiangshang;Button bianji;Button tianjia;Button shanchu;View v;static ArrayList<File[]>jiansuoroot = new ArrayList<File[]>();static List<File>jiansuowenjian = new ArrayList<File>();static File fi;static ArrayList<File>wenjianjia = new ArrayList<File>();static ArrayList<File>ziwenjian = new ArrayList<File>();static ArrayList<File[]>ziwenjian1 = new ArrayList<File[]>();static ArrayList<File[]>rootwenjianjia = new ArrayList<File[]>();//jiansuowen js = new jiansuowen();//zixiancheng xiancheng = null;Handler hanlder;Handler handler = null;Handler childhandler = null;boolean jiansuowanbi = false;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_text); xiancheng = new zixiancheng(); xiancheng.start(); v = (View)findViewById(R.id.view1); intent = this.getIntent(); leixing_xuanzhe = intent.getIntArrayExtra(""leixing_xuanzhe""); list = (ListView)findViewById(R.id.fileslist); parse(leixing_xuanzhe); list.setOnItemClickListener(new mylistitemclick()); xiangshang = (Button)findViewById(R.id.wj_xiangshang); bianji = (Button)findViewById(R.id.wj_bianji); tianjia = (Button)findViewById(R.id.wj_tianjia); shanchu = (Button)findViewById(R.id.wj_shanchu); xiangshang.setOnClickListener(new myClick()); bianji.setOnClickListener(new myClick()); tianjia.setOnClickListener(new myClick()); shanchu.setOnClickListener(new myClick()); //thread = new jiansuothread(this); handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg.what == 111) { progress.cancel(); List<Map<StringObject>>listitems = new ArrayList<Map<StringObject>>(); for(int i= 0;i<jiansuowenjian.size();i++) { Map<StringObject>item = new HashMap<StringObject>(); item.put(""icon""R.drawable.file ); item.put(""name"" jiansuowenjian.get(i).getName()); listitems.add(item); } SimpleAdapter adpter = new SimpleAdapter(TexlisttActivity.thislistitems R.layout.list new String[] {""icon""name""} new int[] {R.id.iconR.id.listitem}); list.setAdapter(adpter); } } };}`
子线程的代码
enter code here`
class zixiancheng extends Thread {
Message msg = null;
public void run() {
Looper.prepare();
childhandler = new Handler() {@Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg.what == 222) { try { for(int i =0; i<wenjianjia.size();i++) { jinasuo1(wenjianjia.get(i)); } jiansuowanbi = true; msg = handler.obtainMessage(); msg.what = 111; handler.sendMessage(msg); } catch (Exception e) { // TODO: handle exception } } } }; Looper.loop(); }}
时间: 2024-12-03 22:04:04