我试图在单击 ListItem 时打开一个对话框,但出现以下错误。
Error : 07-30 14:30:17.295: E/AndroidRuntime(16563): FATAL EXCEPTION: main
07-30 14:30:17.295: E/AndroidRuntime(16563): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.view.ViewRootImpl.setView(ViewRootImpl.java:712)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.app.Dialog.show(Dialog.java:277)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
07-30 14:30:17.295: E/AndroidRuntime(16563): at com.itcuties.multicategoryrssreader.BlogFeeds$RssFeedTask$1.onItemClick(BlogFeeds.java:85)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.widget.AbsListView.performItemClick(AbsListView.java:1283)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.widget.AbsListView$1.run(AbsListView.java:4147)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.os.Handler.handleCallback(Handler.java:615)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.os.Looper.loop(Looper.java:137)
07-30 14:30:17.295: E/AndroidRuntime(16563): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-30 14:30:17.295: E/AndroidRuntime(16563): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 14:30:17.295: E/AndroidRuntime(16563): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 14:30:17.295: E/AndroidRuntime(16563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-30 14:30:17.295: E/AndroidRuntime(16563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-30 14:30:17.295: E/AndroidRuntime(16563): at dalvik.system.NativeStart.main(Native Method)
代码 :
public class BlogFeeds extends Activity {
/** Called when the activity is first created. */
ListView _rssFeedListView;
List<JSONObject> jobs;
List<RssFeedStructure> rssStr;
private BlogAdapter _adapter;
TextView textview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
_rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview);
textview = (TextView) findViewById(R.id.loading);
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
}
private class RssFeedTask extends AsyncTask<String, Void, String> {
// private String Content;
// private ProgressDialog Dialog;
String response = "";
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... urls) {
try {
String feed = "http:urlofawebsite";
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(BlogFeeds.this, rssStr);
_rssFeedListView.setAdapter(_adapter);
textview.setVisibility(View.INVISIBLE);
_rssFeedListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder alert = new AlertDialog.Builder(BlogFeeds.this);
alert.setTitle("Title here");
WebView wv = new WebView(BlogFeeds.this);
wv.loadUrl(rssStr.get(position).getEncodedContent());
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(
WebView view, String url) {
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int id) {
}
});
alert.show();
}
});
}
}
}
}
Shani建议后的错误:
07-30 16:01:15.390: E/AndroidRuntime(623): 致命异常: main 07-30 16:01:15.390: E/AndroidRuntime(623): android.view.WindowManager$BadTokenException: 无法添加窗口 --令牌 android.app.LocalActivityManager$LocalActivityRecord@41c3f030 无效;您的活动正在运行吗?07-30 16:01:15.390: E/AndroidRuntime(623): 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:708) 07-30 16:01:15.390: E/AndroidRuntime(623): 在 android. view.WindowManagerImpl.addView(WindowManagerImpl.java:346) 07-30 16:01:15.390: E/AndroidRuntime(623): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 07-30 16:01: 15.390: E/AndroidRuntime(623): 在 android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 07-30 16:01:15.390: E/AndroidRuntime(623): 在 android.view.Window$LocalWindowManager .addView(Window.java: