致命异常:主,运行时异常无法启动活动

时间:2015-12-14 10:47:24

标签: android-fragments

我想知道这个对象在这里显示为空指针的问题。请澄清我。

这是我的代码:

public class HomeFragment extends Fragment {

    int mFlipping = 0 ; // Initially flipping is off
    Button mButton ;

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);


        View.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ViewFlipper flipper = (ViewFlipper) getActivity().findViewById(R.id.flipper1);

                if (mFlipping == 0) {
                    /** Start Flipping */
                    flipper.startFlipping();
                    mFlipping = 1;
                    mButton.setText(R.string.str_btn_stop);
                } else {
                    /** Stop Flipping */
                    flipper.stopFlipping();
                    mFlipping = 0;
                    mButton.setText(R.string.str_btn_start);
                }
            }
        };

        /** Getting a reference to the button available in the resource */
        mButton = (Button) getActivity().findViewById(R.id.btn);

        /** Setting click event listner for the button */
        mButton.setOnClickListener(listener);

    return rootView;
}
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    }

0 个答案:

没有答案