1

我在 Fragment 中为我的应用程序使用 Caldroid 库。每当我尝试添加一条语句来编辑 Caldroid 片段时,它都会给我一个关于 caldroid 片段的空对象引用。看起来我做了文档所说的一切来设置它。

public class CalendarFragment extends Fragment {

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

public static CalendarFragment getInstance(int position) {
    CalendarFragment myFragment = new CalendarFragment();
    Bundle args = new Bundle();
    args.putInt("position", position);
    myFragment.setArguments(args);
    return myFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CaldroidFragment caldroidFragment = new CaldroidFragment();
    Bundle args = new Bundle();
    Calendar cal = Calendar.getInstance();
    args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
    args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
    args.putBoolean(CaldroidFragment.ENABLE_SWIPE, false);
    args.putBoolean(CaldroidFragment.SHOW_NAVIGATION_ARROWS, true);
    caldroidFragment.setArguments(args);

    FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
    t.replace(R.id.calendarLayout, caldroidFragment);
    t.commit();

    caldroidFragment.getMonthTitleTextView().setTextColor(Color.BLACK);
}

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

    return rootView;
}

这是使其崩溃的行,或具有 caldroidFragment.blala 的任何行

caldroidFragment.getMonthTitleTextView().setTextColor(Color.BLACK);

这是错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference
        at com.gigstudios.habittracker.CalendarFragment.onCreate(CalendarFragment.java:46)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
        at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:846)
        at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:860)
        at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1103)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
        at android.view.View.measure(View.java:17440)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17440)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:17440)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17440)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17440)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17440)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
        at android.view.View.measure(View.java:17440)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1189)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1402)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

非常感谢您的建议!

4

1 回答 1

2

问题是 Caldroid Fragment 尚未初始化。你必须等到初始化 Caldroid 是一个片段并且它有自己的生命周期。caldroid 片段有自己的事件。当片段的视图准备好时,它将触发 onCaldroidViewCreated() 事件。:

caldroidFragment.setCaldroidListener( new CaldroidListener() {

        @Override
        public void onSelectDate(Date date, View view) {
        }

        @Override
        public void onChangeMonth(int month, int year) {
        }

        @Override
        public void onCaldroidViewCreated() {
             super.onCaldroidViewCreated();
             caldroidFragment.getMonthTitleTextView().setTextColor(Color.BLACK);

        }
    } );
于 2015-07-29T08:22:11.350 回答