Android app not responding at Calender.getInstance

你离开我真会死。 提交于 2019-12-24 08:27:11

问题


I am trying to get date doing calculations. For that I'm using Calendar cal = Calendar.getInstance();I am using this import import java.util.Calendar; When the app comes to onResume I am calling a method. In that method, the first line is getting Instance(). But for some reason, I am getting this error(ANR).

    at java.util.Calendar.getInstance(Calendar.java:960)
    at java.util.GregorianCalendar.<init>(GregorianCalendar.java:231)
    at java.util.GregorianCalendar.<init>(GregorianCalendar.java:330)
    at java.util.Calendar.<init>(Calendar.java:718)
    at java.util.Calendar.<init>(Calendar.java:712)   
Caused by: com.github.anrwatchdog.ANRError$$$_Thread: main  
Caused by: com.github.anrwatchdog.ANRError: Application Not Responding
    at com.github.anrwatchdog.ANRWatchDog.void run()(SourceFile:212)
    at com.splunk.mint.Mint$3.void onAppNotResponding(com.github.anrwatchdog.ANRError)(SourceFile:297)
java.lang.Exception: com.github.anrwatchdog.ANRError: Application Not Responding

EDIT (adding code as asked)

public static int getCategory(final String time) {
    long thenTime = SDKUtils.getLong(getLongValue(time));
    Calendar c1 = Calendar.getInstance(); // today
    Calendar c2 = Calendar.getInstance();
    c2.setTime(new Date(thenTime)); // your date
    if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR))
        return TODAY;
    c1.add(Calendar.DAY_OF_YEAR, -1); // yesterday

    if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR))
        return YESTERDAY;
    c1 = Calendar.getInstance();
    if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) )
        return MONTH;
    else
        return YEAR;

}

public static String[] getSubTime(final String time, final int category) {

    Calendar cal = Calendar.getInstance();
    Date date = new Date(SDKUtils.getLong(getLongValue(time)));
    TimeZone tz = TimeZone.getDefault();
    cal.setTime(date);
    cal.setTimeZone(tz);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if (hour > 12)
        hour = hour - 12;
    int min = cal.get(Calendar.MINUTE);
    String hourStr = hour < 10 ? "0" + hour : "" + hour;
    String minStr = min < 10 ? "0" + min : "" + min;
    String am = cal.get(Calendar.HOUR_OF_DAY) < 12 ? " AM" : " PM";
    switch (category) {
    case TODAY:
    case YESTERDAY: {
        return new String[] { hourStr + ":" + minStr + "", am };

    }
    case MONTH: {
        int date_ = cal.get(Calendar.DAY_OF_MONTH);
        String dateString = date_ < 10 ? "0" + date_ : date_ + "";
        return new String[] { dateString, hourStr + ":" + minStr + "" + am };

    }
    case DATE: {
        int date_ = cal.get(Calendar.DAY_OF_MONTH);
        String dateString = date_ < 10 ? "0" + date_ : date_ + "";
        return new String[] { dateString, hourStr + ":" + minStr + "" + am };
    }
    default: {
        String month = convertNumberToMonthMMM(cal.get(Calendar.MONTH));
        int date_ = cal.get(Calendar.DAY_OF_MONTH);
        String dateString = date_ < 10 ? "0" + date_ : date_ + "";
        return new String[] { month + " " + dateString, ", " + hourStr + ":" + minStr + "" + am };
    }
    }

}

These are the two methods i call. what is wrong?


回答1:


Why you not try:

public class MainActivity extends AppCompatActivity
{
    Calendar c = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    }
}

Edit: I think you're using:

import java.util.GregorianCalendar;

You try (add or) instead of:

import java.util.Calendar;

Regards



来源:https://stackoverflow.com/questions/49526832/android-app-not-responding-at-calender-getinstance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!