gpt4 book ai didi

从 Activity 调用 Fragment 时,Android Caldroid Calendar Nullpointerexception 错误

转载 作者:行者123 更新时间:2023-11-30 00:48:48 28 4
gpt4 key购买 nike

我通过 fragment 创建了 Caldroid 日历:

这是我在其中创建日历的 CalendarFragment.java 文件:

package com.petar.android.simplenote;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.petar.android.simplenote.beans.Drop;
import com.roomorama.caldroid.CaldroidFragment;

import java.util.Calendar;
import java.util.Date;

import io.realm.Realm;
import io.realm.RealmResults;


/**
* A simple {@link Fragment} subclass.
*/
public class CalendarFragment extends Fragment {
CaldroidFragment caldroidFragment;

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

View rootView = inflater.inflate(R.layout.calendar_fragment, container, false);
caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt("month", cal.get(Calendar.MONTH) + 1);
args.putInt("year", cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);


//calling this metod will cause to pull data from Realm Data Base and set backgroud for saved dates in database
postaviPozadinskuBojuNota();

FragmentTransaction t = getChildFragmentManager().beginTransaction();
t.replace(R.id.calendar_container, caldroidFragment);
t.commit();
caldroidFragment.refreshView();
return rootView;
}

//pulling data from database and setting backgroud for Date
public void postaviPozadinskuBojuNota() {
Realm realm = null;
try {
Date date;
realm = Realm.getDefaultInstance();
RealmResults<Drop> results = realm.where(Drop.class).equalTo("completed", false).equalTo("deleted", false).findAll(); //posto smo u thredu nemoramo findAllAsyinc
for (Drop current : results) {
if (current.getColorPickerRoudIcon() == R.drawable.ic_drop_black) {
long item_time = current.getWhen();//getting time from Realm Data Base
date = new Date(item_time);
caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.calendarBlack)), date);
}
if (current.getColorPickerRoudIcon() == R.drawable.ic_drop_red) {
long item_time = current.getWhen();//getting time from Realm Data Base
date = new Date(item_time);
caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.calendarRed)), date);
}
if (current.getColorPickerRoudIcon() == R.drawable.ic_drop) {
long item_time = current.getWhen();//getting time from Realm Data Base
date = new Date(item_time);
caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.calendarBlue)), date);
}
if (current.getColorPickerRoudIcon() == R.drawable.ic_drop_yellow) {
long item_time = current.getWhen();//getting time from Realm Data Base
date = new Date(item_time);
caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.calendarYellow)), date);
}
if (current.getColorPickerRoudIcon() == R.drawable.ic_drop_green) {
long item_time = current.getWhen();//getting time from Realm Data Base
date = new Date(item_time);
caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.calendarGreen)), date);
}
if (current.getColorPickerRoudIcon() == R.drawable.ic_drop_white) {
long item_time = current.getWhen();//getting time from Realm Data Base
date = new Date(item_time);
caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.calendarWhite)), date);
}
}
} finally {
if (realm != null) {
realm.close();
}
}
}

//this method is called from activity
public void testUpdateEveryting() {
postaviPozadinskuBojuNota();
caldroidFragment.refreshView();
}
}

我想从 Activity 调用 CalendarFragment.java 中的最后一个方法这是我调用的方法

 //this method is called from activity
public void testUpdateEveryting() {
postaviPozadinskuBojuNota();
caldroidFragment.refreshView();
}

当我像这样单击按钮时,我从 Activity 调用该方法:

CalendarFragment calendarFragment = new CalendarFragment();
calendarFragment.testUpdateEveryting();

我想要完成的是通过调用

更新日期背景
postaviPozadinskuBojuNota();

所以当我按下按钮时,我得到这个错误信息:

FATAL EXCEPTION: main Process: com.petar.android.simplenote, PID: 21735
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference
at android.support.v4.content.ContextCompatApi23.getColor(ContextCompatApi23.java:31)
at android.support.v4.content.ContextCompat.getColor(ContextCompat.java:404)
at com.petar.android.simplenote.CalendarFragment.postaviPozadinskuBojuNota(CalendarFragment.java:79)
at com.petar.android.simplenote.CalendarFragment.testUpdateEveryting(CalendarFragment.java:106)
at com.petar.android.simplenote.ActivityMain.onOptionsItemSelected(ActivityMain.java:351)
at android.app.Activity.onMenuItemSelected(Activity.java:3204)

当我按下按钮时一切都崩溃了,然后调用 CaldroidFragment 中的方法

//this method is called from activity
public void testUpdateEveryting() {
postaviPozadinskuBojuNota();
caldroidFragment.refreshView();
}

然后调用这个方法:

postaviPozadinskuBojuNota();

这条线上的一切都崩溃了

caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable (ContextCompat.getColor(getActivity(), R.color.calendarGreen)), date);

有错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference

at android.support.v4.content.ContextCompatApi23.getColor(ContextCompatApi23.java:31)
at android.support.v4.content.ContextCompat.getColor(ContextCompat.java:404)

最佳答案

当您想与 fragment 通信时,您不会创建它的新实例,如下所示:CalendarFragment calendarFragment = new CalendarFragment();

相反,您使用 findFragmentByTag()findFragmentById() 检索已经创建的 Fragment。

看看这是否对您有帮助:https://developer.android.com/training/basics/fragments/communicating.html

关于从 Activity 调用 Fragment 时,Android Caldroid Calendar Nullpointerexception 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41394421/

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