Android 从 Activity 调用 Fragment 时出现 Caldroid Calendar Nullpointerexception 错误
Android Caldroid Calendar Nullpointerexception error when calling from Activity to 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)
当您想与片段通信时,您不会创建它的新实例,如下所示:CalendarFragment calendarFragment = new CalendarFragment();
.
相反,您使用 findFragmentByTag()
或 findFragmentById()
检索已创建的片段。
看看这是否对您有帮助:https://developer.android.com/training/basics/fragments/communicating.html
我通过片段创建了 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)
当您想与片段通信时,您不会创建它的新实例,如下所示:CalendarFragment calendarFragment = new CalendarFragment();
.
相反,您使用 findFragmentByTag()
或 findFragmentById()
检索已创建的片段。
看看这是否对您有帮助:https://developer.android.com/training/basics/fragments/communicating.html