Android ListView throws NullPointer exception on click-Collection of common programming errors
I have the following code which correctly adds the series name to aName and link to aLink so both ArrayLists hold values. When run the ListView correctly displays aName, but when one of the items in the listview is clicked the app throws the NullPointer exeption.
public class MainActivity extends ListActivity {
ArrayAdapter 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(" ");
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(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);
}
});
}
Here’s the log-
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)
-
You are extending ListActivity, override the method onListItemClick() in Activity instead of view.setOnItemClickListener that you are doing in onCreate()
EDIT:
in your layout xml:
in your activity that uses the layout:
in class: public class
MyActivity extends ListActivity
getting the list view:
ListView list = getListView();
setting the list adapter:
setListAdapter(adapter);
inside MyActivity class override the onListItemClick:
@Override protected void onListItemClick(ListView l, View v, int position, long id) { //do your thing }
documentation: http://developer.android.com/reference/android/app/ListActivity.html
-
You could also create a “normal” Activity instead of a ListActivity.
public class MainActivity extends Activity { ...
Change the ListViews id in your xml layout to “@+id/list”.
Originally posted 2013-11-16 20:50:04.