java - Null object reference when initializing spinners in tablayout -
i have multiple tabs spinners in them, , when switch fragment sliders need initialized corresponding string-arrays. issue keep receiving "null object reference" error. here's code.
fragment code:
public class pageone extends fragment { public static final string arg_page = "arg_page"; private int mpage; public static pageone newinstance(int page) { bundle args = new bundle(); args.putint(arg_page, page); pageone fragment = new pageone(); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mpage = getarguments().getint(arg_page); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mainactivity mainactivity = new mainactivity(); if(mpage>=3) { mainactivity.addshittospinners(mpage); } view view = inflater.inflate(r.layout.fragment_page_one, container, false); //tips if(mpage == 1) { view = inflater.inflate(r.layout.fragment_page_one, container, false); return view; } //main if(mpage == 2) { view = inflater.inflate(r.layout.page_two, container, false); return view; } //auto if(mpage == 3) { view = inflater.inflate(r.layout.page_three, container, false); return view; } //defenses if(mpage == 4) { view = inflater.inflate(r.layout.page_four, container, false); return view; } //shooting if(mpage == 5) { view = inflater.inflate(r.layout.page_five, container, false); return view; } //end game if(mpage == 6) { view = inflater.inflate(r.layout.page_six, container, false); return view; } return view; } }
heres code mainactivity (where spinners initialized):
public class mainactivity extends appcompatactivity {
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar mytoolbar = (toolbar) findviewbyid(r.id.main_toolbar); setsupportactionbar(mytoolbar); tablayout tablayout = (tablayout)findviewbyid(r.id.tabs); viewpager viewpager = (viewpager)findviewbyid(r.id.view_p); viewpager.setadapter(new myfragmentpageradapter(getsupportfragmentmanager(), mainactivity.this)); tablayout.setupwithviewpager(viewpager); } @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); } public void addshittospinners(int i) { system.out.print(i); if(i==3) { spinner startingzone = (spinner) findviewbyid(r.id.auto_start_spinner); arrayadapter<charsequence> adapterstart = arrayadapter.createfromresource(this, r.array.zones, android.r.layout.simple_spinner_item); adapterstart.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); startingzone.setadapter(adapterstart); spinner crossorreachspinner = (spinner) findviewbyid(r.id.cross_or_reach_spinner); arrayadapter<charsequence> adaptercr = arrayadapter.createfromresource(this, r.array.cross_or_reach, android.r.layout.simple_spinner_item); adaptercr.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); crossorreachspinner.setadapter(adaptercr); spinner autodcrossed = (spinner) findviewbyid(r.id.auto_defense_spinner); arrayadapter<charsequence> adapterautodcrossed = arrayadapter.createfromresource(this, r.array.defenses, android.r.layout.simple_spinner_item); adapterautodcrossed.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); autodcrossed.setadapter(adapterautodcrossed); spinner autohimade = (spinner) findviewbyid(r.id.hi_made_auto_spinner); arrayadapter<charsequence> adapterhimade = arrayadapter.createfromresource(this, r.array.shots, android.r.layout.simple_spinner_item); adapterhimade.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); autohimade.setadapter(adapterhimade); spinner autolomade = (spinner) findviewbyid(r.id.lo_made_auto_spinner); arrayadapter<charsequence> adapterlo = arrayadapter.createfromresource(this, r.array.shots, android.r.layout.simple_spinner_item); adapterlo.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); autolomade.setadapter(adapterlo); } if(i==4) { arrayadapter<charsequence> adaptercrosses = arrayadapter.createfromresource(this, r.array.crosses, android.r.layout.simple_spinner_item); adaptercrosses.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner portculliscrosses = (spinner) findviewbyid(r.id.portcullis_spinner); spinner chevalcrosses = (spinner) findviewbyid(r.id.cheval_spinner); spinner rampartscrosses = (spinner) findviewbyid(r.id.ramparts_spinner); spinner moatcrosses = (spinner) findviewbyid(r.id.moat_spinner); spinner drawcrosses = (spinner) findviewbyid(r.id.draw_bridge_spinner); spinner sallycrosses = (spinner) findviewbyid(r.id.sally_port_spinner); spinner rockwallcrosses = (spinner) findviewbyid(r.id.rockwal_spinner); spinner roughcrosses = (spinner) findviewbyid(r.id.rough_terrain_spinner); spinner lowcrosses = (spinner) findviewbyid(r.id.low_bar_spinner); portculliscrosses.setadapter(adaptercrosses); chevalcrosses.setadapter(adaptercrosses); rampartscrosses.setadapter(adaptercrosses); moatcrosses.setadapter(adaptercrosses); drawcrosses.setadapter(adaptercrosses); sallycrosses.setadapter(adaptercrosses); rockwallcrosses.setadapter(adaptercrosses); roughcrosses.setadapter(adaptercrosses); lowcrosses.setadapter(adaptercrosses); } if(i==5) { arrayadapter<charsequence> adaptershots = arrayadapter.createfromresource(this, r.array.shots, android.r.layout.simple_spinner_item); adaptershots.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner himade = (spinner) findviewbyid(r.id.hi_made_spinner); spinner hifail = (spinner) findviewbyid(r.id.hi_failed_spinner); spinner lomade = (spinner) findviewbyid(r.id.lo_made_spinner); himade.setadapter(adaptershots); hifail.setadapter(adaptershots); lomade.setadapter(adaptershots); } if(i==6) { spinner scale = (spinner) findviewbyid(r.id.scale_spinner); spinner capture = (spinner) findviewbyid(r.id.capture_spinner); arrayadapter<charsequence> adapteryorn = arrayadapter.createfromresource(this, r.array.y_or_n, android.r.layout.simple_spinner_item); adapteryorn.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); scale.setadapter(adapteryorn); capture.setadapter(adapteryorn); } }
}
instead of:
mainactivity mainactivity = new mainactivity(); if(mpage>=3) { mainactivity.addshittospinners(mpage); }
use:
if(mpage>=3) { ((mainactivity) getactivity()).addshittospinners(mpage); }
Comments
Post a Comment