gpt4 book ai didi

android - 窗口管理器 $BadTokenException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:23:07 25 4
gpt4 key购买 nike

我正在尝试将进度对话框放在 ListView 的 Click 事件上,如下面的代码所述,但出现错误“WindowManager$BadTokenException:无法添加窗口—— token android.app .LocalActivityManager$LocalActivityRecord@44eddc70 无效;您的 Activity 正在运行吗?”您能给我任何解决方案吗?

代码

 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 类用于更新后台线程中的 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 - 窗口管理器 $BadTokenException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com