我正在尝试在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>
答案 0 :(得分:0)
您需要在自定义适配器的getView()内为imageView定义单击侦听器,即image.setOnClickListener
。
答案 1 :(得分:0)
imageView在new_popup_layout.xml
中定义,但您使用了setContentView(R.layout.listview)
。它无法在指定的layout.xml中找到该imageview。所以返回null
请看一次这件事。