0

我的 Android 应用程序有问题,我的应用程序是ProgressDialog为显示计算数据而构建的,但没有使用“睡眠”

但是该计算的数据结果没有显示

我想用 TextView 和 Listview 显示数据

消息错误是only the original thread that created a view hierarchy can touch its views

这是我的代码:

 progressDialog = ProgressDialog.show(hasilrute.this, "","Communicating",true); 

            new Thread(new Runnable() { 
                 @Override 
                 public void run() { 
                    cariRute(tv);
                    try { 

                    } catch (Exception e) { 
                    // TODO: handle exception 
                   } 
                   handler.sendEmptyMessage(0); 
                progressDialog.dismiss(); 
                 } 
           }).start(); 
          progressDialog.dismiss(); 
           Handler handle=new Handler(){ 
                 public void handleMessage(Message msg) { 
                       super.handleMessage(msg); 
                       if(msg.what == 0) { 
                         //perform some action here................ 
                       } 
                 } 
           }; 

这是我的cariRute方法:

public void cariRute(TextView t){
    String haha = tv.getText().toString().toLowerCase();
    String []getAsalTujuan = haha.split("-");
    getAsal = getAsalTujuan[0];
    getTujuan = getAsalTujuan[1];
    cari(getAsal,getTujuan);


    route = new ArrayList<rute>();
    for(int i=0;i<hasilKahir.size()-1;i++){
        a = hasilKahir.get(i);
        i++;

        b = hasilKahir.get(i);
        i--;

        hitungJarak(a, b);
        System.out.println(a+b);

        List<rute> temp = new ArrayList<rute>();
        temp = getDataRute(a,b);

        /*temp = getDataRute(a,b);
        for(int j = 0; j < temp.size(); j++){
            route.add(temp.get(j));             
        }*/

    }
    List<rute> temp = new ArrayList<rute>();
    temp = filterList(listJalan);
    route = temp;

        jrk = 0;
        for(int k = 0;k<Ljrk.size();k++){

                jrk = jrk + Ljrk.get(k);
            }
        //System.out.println(jrk);
        ongkos = 0; 
        for(int l = 0;l<Longkos.size();l++){

            ongkos = ongkos + Longkos.get(l);
            //System.out.println(ongkos);
        }

        pembulatan("####.##", jrk);
        hitungOngkos(ongkos);
        System.out.println(jrk+ongkos);

    JARAK = (TextView)findViewById(R.id.textView11);
    JARAK.setText("        Jarak : "+keluaran+" km");

    ONGKOS = (TextView)findViewById(R.id.textView12);
    ONGKOS.setText("        Ongkos : Rp."+harga);

    Longkos.clear();


    for(int x = 0; x < route.size();x++){

    }
        ListAdapter adapter = new ruteListAdapter(this, (List<? extends Map<String, String>>) route,
                R.layout.list_item_2, new String[] {
                  rute.ANGKOT, rute.JALAN }, new int[] {
                  R.id.textID, R.id.textRute });
                  this.setListAdapter(adapter);
                  //db.close();

    }

这是我的ruteListAdapter课:

private List<rute> route;
private int[] colors = new int[] { 0x30ffffff, 0x30808080 };
@SuppressWarnings("unchecked")
public ruteListAdapter(Context context, 
    List<? extends Map<String, String>> route, 
    int resource, 
    String[] from, 
    int[] to) {
super(context, route, resource, from, to);
this.route = (List<rute>) route;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}

谁能帮我解决我的问题?

4

2 回答 2

0

尝试这个

progressDialog = ProgressDialog.show(hasilrute.this, "","Communicating",true); 

    new Thread(new Runnable() { 
         @Override 
         public void run() { 
            cariRute(tv);
            youActivityName.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    progressDialog.dismiss();

                }
            });
            try { 

            } catch (Exception e) { 
            // TODO: handle exception 
           } 

         } 
   }).start(); 

编辑

更新 UI 时的另一件事

使用 UI 线程

喜欢:

yourActivityName.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        ONGKOS.setText("        Ongkos : Rp."+harga);
    }
});
于 2012-07-05T16:13:30.057 回答
0

您正在从您创建的新线程中调用 cariRute(tv),这很好。但是,在您的 cariRute() 方法中,您正在尝试更改 TextView 的文本。这种操作(编辑现有视图)必须在主 UI 线程上完成;您正在尝试在后台线程上执行此操作,因此它会给您一个错误。您将不得不更改您的代码,以便视图的更改发生在 UI 线程上。

(例如:让 cariRute(tv) 调用返回您想要显示的文本(也就是除了实际更改文本之外的所有其他操作)然后使用返回值在 UI 线程上设置文本。)

根据您在评论中的解释进行编辑:

你应该看看这个问题。(Android,ListView IllegalStateException:“适配器的内容已更改,但 ListView 没有收到通知”)本质上,如果您要编辑 ListView 的内容(通过更改 ListAdapter),您必须调用 notifyDataSetChanged 以便 listview知道检查数据的变化。

于 2012-07-05T16:16:44.237 回答