シリーズ名をaNameに正しく追加し、aLinkにリンクする次のコードがあるため、両方のArrayListが値を保持します。ListView を実行すると aName が正しく表示されますが、リストビュー内の項目の 1 つがクリックされると、アプリは NullPointer 例外をスローします。
public class MainActivity extends ListActivity {
ArrayAdapter<String> adapter;
ArrayList aName;
ArrayList aLink;
ListView view;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = getListView();
view.setTextFilterEnabled(true);
//CONNECT CODE//
Document main = Jsoup.parse("<html> </html>");
try {
main = Jsoup.connect("http://www.animeseason.com/anime-list/").get();
} catch (IOException e) {
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Connection Error");
alertDialog.setMessage("Could not Connect.");
alertDialog.show();
e.printStackTrace();
}
//PARSE HTML CODE//
aName = new ArrayList();
aLink = new ArrayList();
main = Jsoup.parse(main.getElementsByClass("series_col").toString());
Elements seriesL = main.getElementsByTag("li");
for(Element anime: seriesL){
Elements link = anime.getElementsByTag("a");
aName.add(link.text());
aLink.add(link.attr("href"));
}
//SET VIEW//
adapter = new ArrayAdapter<String>(this, R.layout.list_item, aName);
setListAdapter(adapter);
//ONCLICK
view.setOnItemClickListener(
new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
Intent myIntent = new Intent(view.getContext(), EpisodeListActivity.class);
String name = (String) aName.get(position).toString();
String link = (String) aLink.get(position).toString();
myIntent.putExtra("Anime", name);
myIntent.putExtra("Link", link);
startActivity(myIntent);
}
});
}
これがログです-
07-04 15:08:15.382: W/dalvikvm(3489): threadid=1: thread exiting with uncaught exception (group=0x40a3c1f8)
07-04 15:08:15.406: E/AndroidRuntime(3489): FATAL EXCEPTION: main
07-04 15:08:15.406: E/AndroidRuntime(3489): java.lang.NullPointerException
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.AbsListView.obtainView(AbsListView.java:2035)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.ListView.makeAndAddView(ListView.java:1772)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.ListView.fillDown(ListView.java:672)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.ListView.fillFromTop(ListView.java:732)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.ListView.layoutChildren(ListView.java:1625)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.AbsListView.onLayout(AbsListView.java:1865)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.View.layout(View.java:11278)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.View.layout(View.java:11278)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.View.layout(View.java:11278)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.View.layout(View.java:11278)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.os.Looper.loop(Looper.java:137)
07-04 15:08:15.406: E/AndroidRuntime(3489): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-04 15:08:15.406: E/AndroidRuntime(3489): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 15:08:15.406: E/AndroidRuntime(3489): at java.lang.reflect.Method.invoke(Method.java:511)
07-04 15:08:15.406: E/AndroidRuntime(3489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-04 15:08:15.406: E/AndroidRuntime(3489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-04 15:08:15.406: E/AndroidRuntime(3489): at dalvik.system.NativeStart.main(Native Method)