0

我试图在单击 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:

4

2 回答 2

0

您应该使用 onCreate() 方法保留它:

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

            }
        });}
于 2013-07-30T09:52:40.977 回答
0

这将起作用:

像这样创建弹出窗口

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" />

于 2013-08-01T09:42:28.710 回答