Caldroid函数setBackgroundDrawableForDate上的NullPointException

时间:2016-04-08 09:29:03

标签: android date datetime calendar caldroid

我使用Caldroid Lib在我的Android应用中创建自定义日历,在setBackgroundDrawableForDate中加载日期值时,我正在使用以下代码行。

public class CaldroidSample extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendar_main);

    CaldroidFragment caldroidFragment = new CaldroidFragment();
    Bundle args = new Bundle();
    Calendar cal = Calendar.getInstance();
    args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
    args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));

    caldroidFragment.setArguments(args);caldroidFragment.setBackgroundDrawableForDate(new ColorDrawable(Color.GREEN), ParseDate("28/04/2016"));

    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, caldroidFragment).commit();

}

public Date ParseDate(String date_str)
{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
            "yyyy-MM-dd", Locale.getDefault());
    Date dateStr = null;
    try {
        dateStr = simpleDateFormat.parse(date_str);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return dateStr;
 }

}

我收到以下错误,不知道这里的问题是什么。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vv.melioring.calendarapp/com.vv.melioring.calendarapp.CaldroidSample}: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference
at java.util.Calendar.setTime(Calendar.java:1195)
at com.roomorama.caldroid.CalendarHelper.convertDateToDateTime(CalendarHelper.java:112)
at com.roomorama.caldroid.CaldroidFragment.setBackgroundDrawableForDate(CaldroidFragment.java:433)
at com.vv.melioring.calendarapp.CaldroidSample.onCreate(CaldroidSample.java:66)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
at android.app.ActivityThread.access$800(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5343) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

1 个答案:

答案 0 :(得分:0)

您的日期为28/04/2016,并尝试使用yyyy-MM-dd进行解析。将其更改为dd/MM/yyyy

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());