2

根据 Armaan 的建议编辑代码:在 onPostExecute 崩溃应用程序中调用 ImageView

详细信息:如果我放置关闭按钮并在 onPostExecute 中调用它的 onClickListener,它会崩溃,不知道可能是什么原因:

public class Async 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> {

    String response = "";
    PopupWindow popupWindow;
    ImageView image; 


    @Override
    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(String... urls) {
        try {
            String feed = "http://someurl";
            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);

            _rssFeedListView.setOnItemClickListener(new OnItemClickListener() {

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

                            LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
                            View layout = layoutInflater.inflate(R.layout.new_popup_layout, null); 

                            popupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                            WebView web = (WebView)layout.findViewById(R.id.webView1);
                            image = (ImageView) findViewById(R.id.closebutton);

                            web.getSettings().setJavaScriptEnabled(true);
                            web.getSettings().setLoadWithOverviewMode(true);
                            web.getSettings().setDefaultFontSize(10);
                            web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
                            web.setScrollbarFadingEnabled(true);

                            String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
                            String summary = rssStr.get(position).getEncodedContent();

                            Log.d("String", summary);
                            web.loadDataWithBaseURL(null, String.format(text, summary), "text/html", "UTF-8",null);
                            popupWindow.showAtLocation(view,Gravity.CENTER, 0, 0);

                            image.setOnClickListener(new OnClickListener() {
                            public void onClick(View v)
                     {
                            popupWindow.dismiss(); 
                     } 
             });

                      }
            });



        }

    }

}

}
4

2 回答 2

1

尝试这个:

像这样创建弹出窗口

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

并使用 webview 创建 new_popup_layout 布局文件,如下所示:

         <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="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/btn_dialog" />
    </TableRow>

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="10dp" />

</LinearLayout>

希望能帮助到你!!

于 2013-08-01T08:49:51.457 回答
0

您可以使用自定义对话框,您需要做的就是在自定义对话框的布局文件中放置一个WebView,然后在您的代码中根据您URL在. 有关如何制作自定义对话框,请查看此处 http://www.mkyong.com/android/android-custom-dialog-example/WebViewListView

于 2013-08-01T08:45:01.947 回答