gpt4 book ai didi

android - AsyncTask onPreExecute 进度对话框

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:54 24 4
gpt4 key购买 nike

当 onPreExecute 函数执行时我有一个 AsyncTask,它给我一个异常

** java.lang.IllegalStateException: View com.android.internal.policy.impl.PhoneWindow$DecorView@44ea0e20 has already been added to the window manager.**

当调用 progressDialog 的 show() 方法时。

我的 Activity

public class TopNewsActivity extends ListActivity {

public static final String LOG_TAG = "Infra";
private ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
new BackgroundAsyncTask().execute();
}

public class BackgroundAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(TopNewsActivity.this);
progressDialog.setCancelable(true);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(0);
progressDialog.show();
}

@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... paths) {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

String xml = XMLfunctions.getTopNewsXML();
Document doc = XMLfunctions.XMLfromString(xml);

int numResults = XMLfunctions.numResults(doc);
Log.d(LOG_TAG, "Number of Results: " + numResults);
if ((numResults <= 0)) {
Toast.makeText(TopNewsActivity.this, "No Result Found",Toast.LENGTH_LONG).show();
finish();
}

NodeList nodes = doc.getElementsByTagName("result");

for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();

Element e = (Element) nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("title", XMLfunctions.getValue(e, "title"));
mylist.add(map);
}
return mylist;
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}

protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(TopNewsActivity.this, result, R.layout.list_item, new String[] { "title" }, new int[] { R.id.item_title });
setListAdapter(adapter);
progressDialog.dismiss();

final ListView lv = getListView();

lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView<?> a, View view, final int position, long id) {

HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);

Intent i = new Intent(TopNewsActivity.this, NewsDetails.class);
i.putExtra("content_id", o.get("id"));
i.putExtra("title", o.get("title"));
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

View v = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", i).getDecorView();

// Again, replace the view
TopNewsGroup.group.setContentView(v);

}
});

}

}

public class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}

}
}

请帮助!!!!

最佳答案

progressdialogs 和 contexts 有一个常见的问题,它一直发生在我身上,Android 文档上有一个部分专门针对这个问题。您可能已经使用上下文“this”声明了它,而上下文实际上应该是您的 Java 类的名称后跟“.this”。

dialog = ProgressDialog.show(Example.this, "",
"Doing stuff. Please wait...", true);

这是因为您希望 progressDialog 在主类中显示,而不是在 Async 类中显示。

如果这不能解决问题,您需要发布代码。

关于android - AsyncTask onPreExecute 进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5832699/

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