我在Fragment内为我的应用程序使用Caldroid库。每当我尝试添加一条语句来编辑Caldroid Frgment时,都会在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)
忠告不胜感激!
最佳答案
问题是Caldroid片段尚未初始化。您必须等到初始化之后,Caldroid还是一个Fragment,并且它具有自己的生命周期。 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);
}
} );
关于android - Caldroid空对象引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31038347/