>我有多个带有微调器的选项卡,当您切换到某个片段时,需要使用相应的字符串数组初始化滑块。问题是我不断收到"空对象引用"错误。这是我的代码。
片段代码:
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;
}
}
以下是 MainActivity 的代码(其中微调器被初始化):
公共类 MainActivity 扩展 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 the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a 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);
}
}
}
而不是:
MainActivity mainActivity = new MainActivity();
if(mPage>=3) {
mainActivity.addShitToSpinners(mPage);
}
用:
if(mPage>=3) {
((MainActivity) getActivity()).addShitToSpinners(mPage);
}
与其使用 new MainActivity()
创建一个全新的、不同的MainActivity
实例,不如使用 getActivity()
检索正确的实例。更好的是,学习片段-活动通信模式并利用它。