gpt4 book ai didi

android - AsyncTask 中 ListItem 的 AlertDialog onclick

转载 作者:行者123 更新时间:2023-11-30 03:27:42 26 4
gpt4 key购买 nike

我想访问 ListItem 的异步任务 onClick 中的警报对话框。以下是我传递给 Activity “getbaseContext()”、“ClassName.this”、“this”的引用;但没有任何效果,它会崩溃。任何想法 ?

异步.java

公共(public)类异步扩展 Activity {

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

ListView _rssFeedListView;
List<JSONObject> jobs;
List<RssFeedStructure> rssStr;
private BlogAdapter _adapter;
TextView textview;
Context context;
AlertDialog.Builder builder
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);

//context = getApplicationContext();

_rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview);
builder = new AlertDialog.Builder(getApplicationContext());
textview = (TextView) findViewById(R.id.loading);
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();

_rssFeedListView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
// builder.setCancelable(true);
builder.setTitle("Invitation sent unsuccesful.Please try again !");
// builder.setInverseBackgroundForced(true);

builder.setNegativeButton(
"Ok",null

);


builder.show();


}
});



}


private class RssFeedTask extends AsyncTask<String, Void, String> {
// private String Content;
// private ProgressDialog Dialog;
String response = "";
AlertDialog.Builder alert;


@Override
protected void onPreExecute() {
}

@Override
protected String doInBackground(String... urls) {
try {

String feed = "http://url";
XmlHandler rh = new XmlHandler();
rssStr = rh.getLatestArticles(feed);
} catch (Exception e) {
}
return response;

}

@Override
protected void onPostExecute(String result) {
if (rssStr != null) {

_adapter = new BlogAdapter(Async.this, rssStr);
_rssFeedListView.setAdapter(_adapter);
textview.setVisibility(View.INVISIBLE);


}


}

}

}

错误 :
07-31 16:45:56.615: E/AndroidRuntime(28575): FATAL EXCEPTION: main
07-31 16:45:56.615: E/AndroidRuntime(28575): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.ViewRootImpl.setView(ViewRootImpl.java:712)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.app.Dialog.show(Dialog.java:277)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
07-31 16:45:56.615: E/AndroidRuntime(28575): at com.itcuties.multicategoryrssreader.BlogFeeds$1.onItemClick(BlogFeeds.java:86)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AbsListView.performItemClick(AbsListView.java:1283)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AbsListView$1.run(AbsListView.java:4147)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.os.Handler.handleCallback(Handler.java:615)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.os.Looper.loop(Looper.java:137)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 16:45:56.615: E/AndroidRuntime(28575): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 16:45:56.615: E/AndroidRuntime(28575): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 16:45:56.615: E/AndroidRuntime(28575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-31 16:45:56.615: E/AndroidRuntime(28575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-31 16:45:56.615: E/AndroidRuntime(28575): at dalvik.system.NativeStart.main(Native Method)

Manish & Raji 的建议中收到错误:

07-31 17:53:35.210: E/AndroidRuntime(8442): FATAL EXCEPTION: main
07-31 17:53:35.210: E/AndroidRuntime(8442): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@41bdb010 is not valid; is your activity running?
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.ViewRootImpl.setView(ViewRootImpl.java:708)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.Window$LocalWindowManager.addView(Window.java:554)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.app.Dialog.show(Dialog.java:277)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
07-31 17:53:35.210: E/AndroidRuntime(8442): at com.itcuties.multicategoryrssreader.BlogFeeds$1.onItemClick(BlogFeeds.java:78)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AbsListView.performItemClick(AbsListView.java:1283)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AbsListView$1.run(AbsListView.java:4147)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.os.Handler.handleCallback(Handler.java:615)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.os.Looper.loop(Looper.java:137)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 17:53:35.210: E/AndroidRuntime(8442): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 17:53:35.210: E/AndroidRuntime(8442): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 17:53:35.210: E/AndroidRuntime(8442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-31 17:53:35.210: E/AndroidRuntime(8442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-31 17:53:35.210: E/AndroidRuntime(8442): at dalvik.system.NativeStart.main(Native Method)

最佳答案

在调用异步类之前初始化上下文。

关于android - AsyncTask 中 ListItem 的 AlertDialog onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17967814/

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