2016-04-24 7 views
0

スピナーを含む複数のタブがあり、特定のフラグメントに切り替えると、スライダーを対応するストリング配列で初期化する必要があります。問題は、私は "nullオブジェクト参照"エラーを受け取り続けることです。ここに私のコードです。tablayoutでスピナーを初期化するときのオブジェクト参照のヌル

フラグメントコード:

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); 
    } 
} 

}

+0

'NullPointerException'を取得している行をlogcatに置き、' highlight'を実行してください。 – Vucko

答えて

0

代わりの:

MainActivity mainActivity = new MainActivity(); 
if(mPage>=3) { 
    mainActivity.addShitToSpinners(mPage); 
} 

使用:

if(mPage>=3) { 
    ((MainActivity) getActivity()).addShitToSpinners(mPage); 
} 
0

new MainActivity()MainActivityの全く新しい、個別のインスタンスを作成するのではなく、getActivity()で正しいインスタンスを取得します。さらに良いことに、フラグメントアクティビティcommunication patternを学び、それを悪用してください。