在tablayout中初始化微调器时为空对象引用

时间:2016-04-24 23:40:24

标签: java android android-fragments android-studio-2.0

我有多个带有微调器的选项卡,当你切换到某个片段时,需要使用相应的字符串数组初始化滑块。问题是我一直收到一个" null对象引用"错误。这是我的代码。

片段代码:

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

}

2 个答案:

答案 0 :(得分:0)

而不是:

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

使用:

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

答案 1 :(得分:0)

不是使用'blue'创建MainActivity的全新且独特的实例,而是使用new MainActivity()检索正确的实例。更好的是,学习片段活动communication pattern并利用它。