gpt4 book ai didi

android - Caldroid 空对象引用

转载 作者:行者123 更新时间:2023-11-29 20:40:41 28 4
gpt4 key购买 nike

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

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 Fragment 还没有初始化。你必须等到初始化 Caldroid 也是一个 fragment ,它有自己的生命周期。 Caldroid fragment 有自己的事件。当 fragment 的 View 准备就绪时,它将触发 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com