FragmentStatePagerAdapter没有调用getItem()

时间:2017-08-11 12:46:55

标签: android fragmentstatepageradapter

我已经看到了我的问题的相关帖子,但是,它并没有解决我的问题。 我正试图用3个片段获得滑动视图。 在主要片段中我有这个:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View vue = inflater.inflate(R.layout.fragment_conversions, container, false);

    ConversionsPagerAdapter cPageAdapter = new ConversionsPagerAdapter(getActivity().getSupportFragmentManager());
    ViewPager vp = vue.findViewById(R.id.conversionsPager);
    cPageAdapter.getItem(0);
    vp.setAdapter(cPageAdapter);


   Button convKmhKts = (Button) vue.findViewById(R.id.convKmhKts);
   convKmhKts.setText("...");
...

这是我的FragmentStatePagerAdapter的类:

public class ConversionsPagerAdapter extends FragmentStatePagerAdapter{
    public ConversionsPagerAdapter(FragmentManager fm) {
        super(fm);
        Log.i("ARKA", "FragmentStatePagerAdapter CONSTRUCTOR");
    }

    @Override
    public Fragment getItem(int i) {
        return ConversionFragment.newInstance(i);
    }

    @Override
    public int getCount() {
        return 1;
    }
}

并显示显示标签的片段:

public class ConversionFragment extends Fragment {
    public static final String ID_CONVERSION = "id_conversion";

    public static ConversionFragment newInstance(int pos) {
        ConversionFragment stf = new ConversionFragment();
        Bundle args = new Bundle();
        args.putInt("pos", pos);
        stf.setArguments(args);
        return stf;
    }


    public ConversionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.

        Bundle args = getArguments();
        int idConversion = args.getInt(ID_CONVERSION);
        Log.i("idConversion", String.valueOf(idConversion));
        int idVue;

        switch (idConversion){
            case 0: idVue = R.layout.fragment_conversions_vitesse;
                break;
            case 1: idVue = R.layout.fragment_conversions_vitesse;
            default: idVue = R.layout.fragment_conversions_vitesse;
                break;
        }

        View rootView = inflater.inflate(idVue, null);
        return rootView;
    }
}

事实是,从不调用getItem()。 我尝试通过getChildFragmentManager()更改getActivity()。getSupportFragmentManager(),但似乎这不是好方法...... 当我试图访问我的按钮时,我得到了这个:

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'

在我正在使用按钮的行。

2 个答案:

答案 0 :(得分:2)

如果您在片段内使用ViewPager,则必须使用chield片段管理器,因此请尝试将getActivity().getSupportFragmentManager()替换为getChildFragmentManager()。如果您使用一个片段管理器来处理所有内容,它会给您带来一些错误。

For more information

答案 1 :(得分:0)

确定, 问题是我试图访问一个没有膨胀的组件:

buttonprocess.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String s = edittextkata.getText().toString();
        String kunci = editkey.getText().toString();

        if (s.equals("")) {
            Toast.makeText(getApplicationContext(), "CAN NOT ENCRYPT! Plaintext is empty.", Toast.LENGTH_SHORT).show();
        } else if (kunci.equals("")) {
            Toast.makeText(getApplicationContext(), "CAN NOT ENCRYPT! KEY is empty", Toast.LENGTH_SHORT).show();
        } else {
            int k = Integer.parseInt(editkey.getText().toString());
            String str = edittextkata.getText().toString();

            if (k < 1) {
                throw new IllegalArgumentException("k must be greater than 0");
            }
            StringBuilder result = new StringBuilder();

            int previous = 0;
            for (int i = k - 1; i < str.length(); i += k) {
                result.append(str, previous, i);
                previous = i + 1;
            }
            result.append(str, previous, str.length());
            return result.toString();

            //Show the result to edittext
            edittexthasil.setText(result);
        }

我在子片段中访问它,现在没问题....