0

我现在坐了几个小时来运行进度对话框....

我在 stackoverflow 和其他网站上查看了很多示例。

问题:

我正在将进度对话框放入活动中,并在按下按钮时将其移交给异步任务。按下按钮时,活动显示大约 2-3 秒而没有进度对话框,在切换到其他活动后,进度对话框显示并在异步任务完成后终止。Coreographer 告诉我,主要活动.. bla bla ..

// Get Position Button
    getPosition = (Button) vg.findViewById(R.id.getPosition);
    getPosition.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isAirplaneModeOn(PositionActivity.this) != true) {
                try {
                    // System.out.println("Airplanemode off!");
                    if (gpsFunc.canGetLocation()) {
                        SharedPreferences gps = PositionActivity.this
                                .getSharedPreferences(prefName,
                                        Context.MODE_PRIVATE);
                        String latlon = gps.getString("coordinates", null);

                        if (latlon != null) {
                            String[] split = latlon.split(";");
                            callWienerLinien();
                            latitude = Double.parseDouble(split[0]);
                            longitude = Double.parseDouble(split[1]);
                            pressed = (TextView) vg
                                    .findViewById(R.id.pressed);
                            long start = new Date().getTime();
                            TabActivity tabs = (TabActivity) getParent();
                            tabs.getTabHost().setCurrentTab(1);
                            long end = new Date().getTime();

                            System.out.println(end-start);

                        } else {
                            pressed.setText("Bitte Position setzen.");
                        }
                    } else {
                        buildAlertMessageNoDataNetwork();
                    }
                } catch (Exception e) {
                    errorOccuredMessage();
                }
            } else {
                pressed.setText("Bitte Flugzeugmodus deaktivieren.");
            }
        }

    });

public void callWienerLinien(){
    ProgressDialog pd = new ProgressDialog(this);
    pd.setMessage("Loading ...");
    pt = new PublicTransport(pd,this);
    pt.execute("http://webservice.qando.at/2.0/webservice.ft");
}

这是异步任务

    public PublicTransport(ProgressDialog pd,Context context){
    this.pd = pd;
    this.context = context;
    getLatLonDestination();
    getLatLonOrigin();
}



    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        System.out.println("onpre");
        pd.show();          
    }


        @Override
    protected void onPostExecute(ArrayList<PublicTransportBean> al) {
        System.out.println(al.get(0).getTripDuration());
        System.out.println("onpostExec");
        pd.dismiss();
    }
4

2 回答 2

1

不要在运行后台任务的线程中显示 Progressdialog。我遇到了同样的问题并做了以下事情:

  1. 准备对话
  2. 显示对话框
  3. 启动后台任务

编辑:

尝试如下调用 ProgressDialog:

      runOnUiThread(new Runnable() {              
            @Override
            public void run() {
            final ProgressDialog progressDialog=new ProgressDialog(cont);
            progressDialog.setMessage("Progress");
            progressDialog.show();
            }
        });
于 2013-07-09T18:20:42.517 回答
0

我解决了这个问题....

我打电话给

 TabActivity tabs = (TabActivity) getParent();
                    tabs.getTabHost().setCurrentTab(1);

在异步任务的后执行中。工作完美.....两行代码和13个小时的学习:-(

于 2013-07-10T09:01:32.150 回答