1

フラグメントを介して 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)
4

1 に答える 1

1

フラグメントと通信したい場合は、次のように新しいインスタンスを作成しませんCalendarFragment calendarFragment = new CalendarFragment();

findFragmentByTag()代わりに、またはを使用して、作成済みの Fragment を取得しますfindFragmentById()

これが役立つかどうかを確認してください: https://developer.android.com/training/basics/fragments/communicating.html

于 2016-12-30T10:43:20.660 に答える