我通过片段创建了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中调用last方法 这是我称之为
的方法 //this method is called from activity
public void testUpdateEveryting() {
postaviPozadinskuBojuNota();
caldroidFragment.refreshView();
}
当我点击这样的按钮时,我在Activity中调用了metod:
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)
答案 0 :(得分:1)
如果您想与片段进行通信,则无需创建新的实例,例如:CalendarFragment calendarFragment = new CalendarFragment();
。
而是使用findFragmentByTag()
或findFragmentById()
检索已创建的片段。
看看这是否对您有所帮助:https://developer.android.com/training/basics/fragments/communicating.html