我正在尝试在 ListView 的 setOnItemClickListner 中使用 image.setOnClickListener 但它崩溃了,知道吗?还是可以在 AsyncTask 的 onPostExecute 里面使用。我这样做的原因是这个图像与我在 AsyncTask 的 onPostExecute 中定义的 LayoutInflater 相关联,所以我必须只保留图像按钮。
错误 :
08-02 15:52:12.829: E/AndroidRuntime(32221): FATAL EXCEPTION: main
08-02 15:52:12.829: E/AndroidRuntime(32221): java.lang.NullPointerException
08-02 15:52:12.829: E/AndroidRuntime(32221): at com.itcuties.multicategoryrssreader.BlogFeeds$RssFeedTask$1.onItemClick(BlogFeeds.java:103)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.widget.AbsListView.performItemClick(AbsListView.java:1283)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.widget.AbsListView$1.run(AbsListView.java:4147)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.os.Handler.handleCallback(Handler.java:615)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.os.Handler.dispatchMessage(Handler.java:92)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.os.Looper.loop(Looper.java:137)
08-02 15:52:12.829: E/AndroidRuntime(32221): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-02 15:52:12.829: E/AndroidRuntime(32221): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 15:52:12.829: E/AndroidRuntime(32221): at java.lang.reflect.Method.invoke(Method.java:511)
08-02 15:52:12.829: E/AndroidRuntime(32221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-02 15:52:12.829: E/AndroidRuntime(32221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-02 15:52:12.829: E/AndroidRuntime(32221): at dalvik.system.NativeStart.main(Native Method)
Popup.java
public class Popup extends Activity {
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 = "";
ImageView image = (ImageView) findViewById(R.id.closebutton);
PopupWindow popupWindow;
@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(Popup.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);
ImageView 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();
}
});
}
});
}
}
}
}
new_popup_layout.xml
<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">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<ImageView
android:id="@+id/closebutton"
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="480dp"/>
</LinearLayout>