在FragmentTransaction中,片段getView()方法为NULL

时间:2018-08-10 10:56:12

标签: android

我在fragment中有两个LinearLayout。 当我单击FAB按钮时,我想隐藏一个,但一个  空对象引用出现在FragmentTransaction隐藏方法上。 为什么“ f”没有视图引用?

public class FragmentOne extends Fragment {

    public FragmentOne() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View returnView = inflater.inflate(R.layout.fragment_one, container, false);

        FloatingActionButton fabTemp = returnView.findViewById(R.id.fabTemp);

        final Fragment f = getActivity().getSupportFragmentManager().getFragments().get(0).getChildFragmentManager().getFragments().get(1);
        final FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

        fabTemp.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {

              transaction.setCustomAnimations(android.R.animator.fade_in,
                android.R.animator.fade_out);

                if (f.isHidden()) {
                    transaction.show(f);
                } else {
                    transaction.hide(f);
                }
                transaction.commit();
            }
        });

        return returnView;
    }

尽管不是我想要的那样,我还是用另一种方式解决了

最终视图frag = getView()。findViewById(R.id.frgSettemp);

frag.setVisibility(View.GONE);

2 个答案:

答案 0 :(得分:0)

onCreateView中,将其充气并返回视图。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View returnView = inflater.inflate(R.layout.fragment_one, container, false);
        return returnView;
    }

尝试使用onViewCreated @override方法进行其他处理。像这样

@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);


        FloatingActionButton fabTemp = getView().findViewById(R.id.fabTemp);

        final Fragment f = getActivity().getSupportFragmentManager().getFragments().get(0);
        final FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();

        fabTemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

        transaction.setCustomAnimations(android.R.animator.fade_in,
                android.R.animator.fade_out);

                if (f.isHidden()) {
                    transaction.show(f);
                } else {
                    transaction.hide(f);
                }
                transaction.commit();
             }
        });

}

希望这会有所帮助。

答案 1 :(得分:0)

您应该只拨打

final Fragment f = getActivity().getSupportFragmentManager().getFragments().get(0);

final Fragment f = getActivity().getSupportFragmentManager().getFragments().get(1);

您所做的肯定会返回null。