如何在AsyncTask中的弹出窗口中打开WebView

时间:2013-08-01 08:39:56

标签: android webview

根据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(); 
                     } 
             });

                      }
            });



        }

    }

}

}

2 个答案:

答案 0 :(得分:1)

试试这个:

像这样创建Popupwindow

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>

希望它能帮助!!

答案 1 :(得分:0)

您可以使用自定义对话框,只需在自定义对话框的布局文件中放置WebView,然后在代码中根据您的URLWebView提供来源ListView。 {{1}}中的所选项目。有关如何制作自定义对话框,请查看此处 http://www.mkyong.com/android/android-custom-dialog-example/