I am trying to open a dialog on click of ListItem, but I am getting below error.
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)
Code :
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();
}
});
}
}
}
}
Error after Shani's suggestion :
07-30 16:01:15.390: E/AndroidRuntime(623): FATAL EXCEPTION: main 07-30 16:01:15.390: E/AndroidRuntime(623): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@41c3f030 is not valid; is your activity running? 07-30 16:01:15.390: E/AndroidRuntime(623): at android.view.ViewRootImpl.setView(ViewRootImpl.java:708) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.view.Window$LocalWindowManager.addView(Window.java:554) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.app.Dialog.show(Dialog.java:277) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.app.AlertDialog$Builder.show(AlertDialog.java:932) 07-30 16:01:15.390: E/AndroidRuntime(623): at com.itcuties.multicategoryrssreader.BlogFeeds$1.onItemClick(BlogFeeds.java:76) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.widget.AdapterView.performItemClick(AdapterView.java:298) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.widget.AbsListView.performItemClick(AbsListView.java:1283) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.widget.AbsListView$1.run(AbsListView.java:4147) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.os.Handler.handleCallback(Handler.java:615) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.os.Handler.dispatchMessage(Handler.java:92) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.os.Looper.loop(Looper.java:137) 07-30 16:01:15.390: E/AndroidRuntime(623): at android.app.ActivityThread.main(ActivityThread.java:4898) 07-30 16:01:15.390: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 16:01:15.390: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511) 07-30 16:01:15.390: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 07-30 16:01:15.390: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 07-30 16:01:15.390: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)