java - getResources array xml in pageradapter -
i try pass value xml array
listfragment
.
public class tabspageradapter extends fragmentpageradapter { static context context; static resources res = null; static string[] content = null; public tabspageradapter(fragmentmanager fm, context c) { super(fm); context = c; } @override public listfragment getitem(int index) { res = context.getresources(); content = res.getstringarray(r.array.trg_values); listfragment fragment = new daerahfragment(); bundle args = new bundle(); args.putstring("daerah", content[index] ); fragment.setarguments(args); return fragment; } @override public int getcount() { // item count - equal number of tabs return 7; } }
here xml
<string-array name="trg_values"> <item name="kuala terengganu">1</item> <item name="marang">2</item> <item name="dungun">3</item> <item name="kemaman">4</item> <item name="hulu terengganu">5</item> <item name="setiu">6</item> <item name="besut">7</item> </string-array>
error:
08-03 16:12:00.992: e/androidruntime(28017): java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources android.content.context.getresources()' on null object reference 08-03 16:12:00.992: e/androidruntime(28017): @ com.jkmt.ppbanjir.adapter.tabspageradapter.getitem(tabspageradapter.java:62)
adapter's instantiation
madapter = new tabspageradapter(getsupportfragmentmanager(), null); viewpager.setadapter(madapter);
the issue line
madapter = new tabspageradapter(getsupportfragmentmanager(), null);
you passing null context
, , assigning null member in adapter. when getitem called, , member referenced, app crashes nullpointerexception
. change with
madapter = new tabspageradapter(getsupportfragmentmanager(), this);
if inside activity
, or
madapter = new tabspageradapter(getsupportfragmentmanager(), getactivity());
if inside fragment
Comments
Post a Comment