0
votes

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)

2
don't open dialog on post execute method..and call setonitemclickListner methos outside post execute method.Shani Goriwal
Shani - Thanks, can you tell me exactly which place i should be keeping it and calling it ?user45678
Did you tried debugging it? Add a try catch in the onclicklistener function and check what is the exception message.Rahul Sundar

2 Answers

0
votes

You should keep it with onCreate() method:

@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();



_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();

            }
        });}
0
votes

This will work :

Create Popupwindow Like this

LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
        View layout = layoutInflater.inflate(R.layout.new_popup_layout, null);  
        final PopupWindow popupWindow = new PopupWindow(
                layout, 
                   LayoutParams.WRAP_CONTENT,  
                         LayoutParams.WRAP_CONTENT);

WebView web = (WebView)layout.findViewbyId(R.id.webView1);
popupWindow.showAtLocation(view,Gravity.CENTER, 0, 0);
and create new_popup_layout layout file with webview like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="461dp" />