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>