无法在子片段上运行方法

时间:2016-03-29 11:17:03

标签: android android-fragments fragment-tab-host

我有一个托管FragmentTabHost的片段。在每个选项卡更改事件中,我想从子片段运行一个方法。但该方法总是抛出空指针异常。 这是我的片段,其中包含FragmentTabHost

public class OverviewFragment extends Fragment {

    private Context mContext;

    private FragmentTabHost mTabHost;

    private EventFragment mEventFragment;
    private AttendanceFragment mAttendanceFragment;
    private InboxFragment mMessageFragment;

    public OverviewFragment() {
        mContext = getContext();
    }

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static OverviewFragment newInstance() {

        Bundle args = new Bundle();

        OverviewFragment fragment = new OverviewFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static OverviewFragment newInstance(Bundle args) {

        OverviewFragment fragment = new OverviewFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_overview, container, false);

        mContext = getContext();

        mEventFragment = EventFragment.newInstance();
        mAttendanceFragment = AttendanceFragment.newInstance();
        mMessageFragment = InboxFragment.newInstance();

        mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(mContext, getChildFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Events"),
                mEventFragment.getClass(), null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Messages"),
                mMessageFragment.getClass(), null);
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Attendance"),
                mAttendanceFragment.getClass(), null);

        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                Bundle args = getArguments();

                switch (tabId) {
                    case "tab1":
                        try {
                            mEventFragment.updateView();
                        } catch (NullPointerException e) {
                            Log.e("EventFragment", "Fragment not loaded");
                        }
                        break;
                    case "tab2":
                        try {
                            mMessageFragment.updateView();
                        } catch (NullPointerException e) {
                            Log.e("MessageFragment", "Fragment not loaded");
                        }
                        break;
                    case "tab3":
                        try {
                            mAttendanceFragment.updateView();
                        } catch (NullPointerException e) {
                            Log.e("AttendanceFragment", "Fragment not loaded");
                        }
                }
            }
        });

        return rootView;
    }

}

这是我的孩子片段:

public class InboxFragment extends Fragment {

    private Context mContext;
    private StaticListView mMessageListView;
    private DatabaseHandler mDatabaseHandler;
    private MessageListAdapter mMessageListAdapter;
    private ArrayList<Message> mMessageList;
    private TextView mFragmentTitle;
    public boolean isLoaded = false;

    public InboxFragment() {
    }

    public static InboxFragment newInstance() {
        Bundle args = new Bundle();

        InboxFragment fragment = new InboxFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static InboxFragment newInstance(Bundle args) {
        InboxFragment fragment = new InboxFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_detail, container, false);

        mContext = getContext();

        updateView();
        return rootView;
    }

    public void updateView() {
        Bundle args = getArguments();

        LocalDate selectedDate;

        if (args != null) {
            int selectedYear = args.getInt(Application.KEY_YEAR),
                    selectedMonth = args.getInt(Application.KEY_MONTH),
                    selectedDay = args.getInt(Application.KEY_DAY);

            selectedDate = new LocalDate(
                    selectedYear,
                    selectedMonth,
                    selectedDay
            );
        } else {
            selectedDate = LocalDate.now();
        }
    }
}

致电mInboxFragment.updateView()始终会返回NullPointerException。所有其他课程也是如此。

1 个答案:

答案 0 :(得分:0)

您应该使用界面。或者这个方法应该是静态方法。否则它将始终返回null。