0

我正在使用 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) 
4

1 回答 1

0

您的日期是28/04/2016,您尝试使用yyyy-MM-dd. 将其更改为dd/MM/yyyy.

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
于 2016-04-08T09:30:55.103 回答