Attempt to invoke virtual method 'void android.widget.ListView.setAdapter -
i trying make list view contains pictures , text within fragment on viewpager tabs, can swipe left or right bring fragment.
i'm relatively new android may doing wrong. can build project fine when emulator runs crashes (log displayed @ bottom of page).
i created separate application , able create list view, when try combine within fragment so, doesn't work. here following code
mainactivity.java
import java.util.arraylist; import java.util.list; import java.util.locale; import android.app.listfragment; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.app.fragmentpageradapter; import android.os.bundle; import android.support.v4.view.viewpager; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class mainactivity extends actionbaractivity implements actionbar.tablistener { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link fragmentpageradapter} derivative, keep every * loaded fragment in memory. if becomes memory intensive, * may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ viewpager mviewpager; private list<posts> myposts = new arraylist<posts>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // set action bar. final actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // create adapter return fragment each of 3 // primary sections of activity. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); // when swiping between different sections, select corresponding // tab. can use actionbar.tab#select() if have // reference tab. mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); // each of sections in app, add tab action bar. (int = 0; < msectionspageradapter.getcount(); i++) { // create tab text corresponding page title defined // adapter. specify activity object, implements // tablistener interface, callback (listener) when // tab selected. actionbar.addtab( actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } intent intent = new intent(mainactivity.this, notmainactivity.class); startactivity(intent); finish(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { // when given tab selected, switch corresponding page in // viewpager. mviewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // return placeholderfragment (defined static inner class below). switch (position){ case 0: return new thewallfragment(); case 1: return new peekfragment(); case 2: return new camerafragment(); default: break; } return placeholderfragment.newinstance(position + 1); } @override public int getcount() { // show 3 total pages. return 3; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getstring(r.string.title_section1).touppercase(l); case 1: return getstring(r.string.title_section2).touppercase(l); case 2: return getstring(r.string.title_section3).touppercase(l); } return null; } } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; /** * returns new instance of fragment given section * number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.wall_layout, container, false); return rootview; } } }
notmainactivity.java
public class notmainactivity extends fragmentactivity { private list<posts> myposts = new arraylist<posts>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); populatepostlist(); populatelistview(); registerclickcallback(); system.out.print("main activity started"); } private void populatepostlist() { myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); myposts.add(new posts("ottawa, ontario", "787", "picture of dog!", r.drawable.dog)); } private void populatelistview() { arrayadapter<posts> adapter = new mylistadapter(); listview list = (listview) findviewbyid(android.r.id.list); list.setadapter(adapter); } //handles clicks on list items private void registerclickcallback(){ listview list = (listview) findviewbyid(android.r.id.list); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { posts clickedpost = myposts.get(position); string message = "you clicked" + position +"location of post is" + clickedpost.getpostlocation(); toast.maketext(notmainactivity.this, message, toast.length_long).show(); } }); } private class mylistadapter extends arrayadapter<posts> { public mylistadapter() { super(notmainactivity.this, r.layout.item_view, myposts); } @override public view getview(int position, view convertview, viewgroup parent) { // make sure have view work {may have given null} view itemview = convertview; if (itemview != null) { itemview = getlayoutinflater().inflate(r.layout.item_view, parent, false); } // find post work with. posts currentpost = myposts.get(position); //fill view imageview imageview = (imageview)itemview.findviewbyid(r.id.item_postimage); imageview.setimageresource(currentpost.geticonid()); //fill in title textview titletext = (textview) itemview.findviewbyid(r.id.item_posttext); titletext.settext(currentpost.getposttitle()); //set vote number textview votetext = (textview) itemview.findviewbyid(r.id.item_postvotetext); votetext.settext(currentpost.getpostvote()); return itemview; } } }
posts.java
public class posts { private string postlocation; private string postvote; private string posttitle; private int iconid; public posts(string postlocation, string postvote, string posttitle, int iconid) { this.postlocation = postlocation; this.postvote = postvote; this.posttitle = posttitle; this.iconid = iconid; } public string getpostlocation() { return postlocation; } public string getpostvote() { return postvote; } public string getposttitle() { return posttitle; } public int geticonid() { return iconid; }
thewallfragment.java
public class thewallfragment extends android.support.v4.app.listfragment { list<posts> myposts = new arraylist<posts>(); private string[] strlistview; private listview mylistview; private int number; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.wall_layout, container, false); return rootview; } }
activity_main.xml
<android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" />
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gnumbu.errolgreen.testing" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".notmainactivity"> </activity> </application> </manifest>
item_view.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/item_postimage" android:src="@drawable/dog" android:maxwidth="80dp" android:minheight="80dp" android:adjustviewbounds="true" android:layout_marginleft="14dp" android:layout_marginstart="14dp" android:layout_alignparenttop="true" android:layout_torightof="@+id/item_upvotearrow" android:layout_toendof="@+id/item_upvotearrow" android:layout_alignbottom="@+id/item_downvotearrow" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this dog! dog amazing, give lick!" android:id="@+id/item_posttext" android:paddingstart="5dp" android:layout_above="@+id/item_downvotearrow" android:layout_torightof="@+id/item_postimage" android:layout_toendof="@+id/item_postimage" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/item_upvotearrow" android:src="@drawable/upvotearrow" android:maxheight="24dp" android:maxwidth="24dp" android:adjustviewbounds="true" android:layout_alignparenttop="true" android:layout_alignleft="@+id/item_downvotearrow" android:layout_alignstart="@+id/item_downvotearrow" android:clickable="true" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/item_downvotearrow" android:src="@drawable/downvotearrow" android:maxwidth="24dp" android:maxheight="24dp" android:adjustviewbounds="true" android:layout_marginleft="9dp" android:layout_marginstart="9dp" android:layout_below="@+id/item_postvotetext" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:longclickable="false" android:clickable="true" /> <textview android:layout_height="wrap_content" android:text="22" android:id="@+id/item_postvotetext" android:maxheight="20dp" android:focusableintouchmode="false" android:visibility="visible" android:singleline="true" android:gravity="center" android:enabled="true" android:layout_below="@+id/item_upvotearrow" android:layout_alignleft="@+id/item_upvotearrow" android:layout_alignstart="@+id/item_upvotearrow" android:nestedscrollingenabled="false" android:layout_width="wrap_content" android:layout_tostartof="@+id/item_postimage" /> </relativelayout>
wall_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new text" android:id="@+id/textview" android:layout_gravity="center_horizontal" /> <listview android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
error log
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5223) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference @ com.gnumbu.errolgreen.testing.notmainactivity.populatelistview(notmainactivity.java:70) @ com.gnumbu.errolgreen.testing.notmainactivity.oncreate(notmainactivity.java:43) @ android.app.activity.performcreate(activity.java:5937) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) at android.app.activitythread.handlelaunchactivity(activitythread.java:2360) at android.app.activitythread.access$800(activitythread.java:144) at android.app.activitythread$h.handlemessage(activitythread.java:1278) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5223) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) at com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
new error
process: com.gnumbu.errolgreen.testing, pid: 5304 java.lang.nullpointerexception: attempt invoke virtual method 'int android.view.view.getimportantforaccessibility()' on null object reference @ android.widget.abslistview.obtainview(abslistview.java:2360) @ android.widget.listview.measureheightofchildren(listview.java:1270) @ android.widget.listview.onmeasure(listview.java:1182) @ android.view.view.measure(view.java:17430) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436) @ android.widget.linearlayout.measurevertical(linearlayout.java:722) @ android.widget.linearlayout.onmeasure(linearlayout.java:613) @ android.view.view.measure(view.java:17430) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463) @ android.widget.framelayout.onmeasure(framelayout.java:430) @ android.view.view.measure(view.java:17430) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436) @ android.widget.linearlayout.measurevertical(linearlayout.java:722) @ android.widget.linearlayout.onmeasure(linearlayout.java:613) @ android.view.view.measure(view.java:17430) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5463) @ android.widget.framelayout.onmeasure(framelayout.java:430) @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2560) @ android.view.view.measure(view.java:17430) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2001) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1166) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1372) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1054) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5779) @ android.view.choreographer$callbackrecord.run(choreographer.java:767) @ android.view.choreographer.docallbacks(choreographer.java:580) @ android.view.choreographer.doframe(choreographer.java:550) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:753) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
notmainactivity
calls setcontentview
r.layout.activity_main
parameter contains viewpager
, not listview
. since don't have listview declare in layout, findviewbyid(android.r.id.list)
returns null, , when try access (calling setadapter
) nullpointerexception
thrown.
in myadapter
, change from
if (itemview != null) { itemview = getlayoutinflater().inflate(r.layout.item_view, parent, false); }
to
if (itemview == null) { itemview = getlayoutinflater().inflate(r.layout.item_view, parent, false); }
Comments
Post a Comment