我正在尝试在ListView的Click事件上添加进度对话框,如下面的代码中所述,但是出现错误“WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44eddc70 is not valid; is your activity running?”,您能为此提供任何解决方案吗?

代码

 final ListView lv1 = (ListView) findViewById(R.id.list);
    lv1.setAdapter(new EfficientAdapter(this));

    lv1.setTextFilterEnabled(true);

    lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v,
                final int position, long id) {
            final ProgressDialog pd = ProgressDialog.show(Add_Entry.this,
                    "", "Please Wait....");
            new Thread() {
                public void run() {

                    if (lv1.getItemAtPosition(position).equals(0)) {

                        Intent edit = new Intent(getApplicationContext(),
                                SourceOfStress.class);
                        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                        edit.putExtra("currActi", "AddEntry");
                        parentActivity.startChildActivity("SorceOfStress",
                                edit);

                    }
                    if (lv1.getItemAtPosition(position).equals(1)) {
                        Intent edit = new Intent(getParent(),
                                SourceOFSymptoms.class);
                        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                        edit.putExtra("currActi", "AddEntry");
                        parentActivity.startChildActivity(
                                "SourceOFSymptoms", edit);
                    }
                    if (lv1.getItemAtPosition(position).equals(2)) {
                        Intent edit = new Intent(getParent(),
                                Stress_Resilliance.class);
                        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                        edit.putExtra("currActi", "AddEntry");
                        parentActivity.startChildActivity(
                                "Stress_Resilliance", edit);
                    }
                    pd.dismiss();
                }
            }.start();
        }

    });

我的文件名为Add_Entry.java
错误排成一行
ProgressDialog.show(Add_Entry.this,
                    "", "Please Wait....");

最佳答案

您正在尝试从线程更新UI。你不能那样做。

使用Handler mechanism更新UI组件。

从网站获取的代码:这里的Handler类用于更新后台Thread中的ProgressBar View 。

package de.vogella.android.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProgressTestActivity extends Activity {
  private Handler handler;
  private ProgressBar progress;
  private TextView text;


/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    progress = (ProgressBar) findViewById(R.id.progressBar1);
    text = (TextView) findViewById(R.id.textView1);

  }

  public void startProgress(View view) {
    // Do something long
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        for (int i = 0; i <= 10; i++) {
          final int value = i;
          try {
            Thread.sleep(2000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          progress.post(new Runnable() {
            @Override
            public void run() {
              text.setText("Updating");
              progress.setProgress(value);
            }
          });
        }
      }
    };
    new Thread(runnable).start();
  }

}

关于android - WindowManager $ BadTokenException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6977771/

10-11 20:14