1

I created Caldroid calendar via fragment:

This is my CalendarFragment.java file where i create calendar:

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();
    }
}

From Activity i want to call last method in CalendarFragment.java This is the method i call

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

And i call that metod from Activity when i click button like this:

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

What i want accomplish is to update backgroud for date by calling

postaviPozadinskuBojuNota();

So when i press button i get this error message:

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)

Everything getting crashed when i press button, then call method in CaldroidFragment

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

Then this method is called:

postaviPozadinskuBojuNota();

And everything get crashed on this line

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

with error:

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)
4

1 回答 1

1

当您想与片段通信时,您不需要创建它的新实例,如下所示CalendarFragment calendarFragment = new CalendarFragment();

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

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

于 2016-12-30T10:43:20.660 回答