使用日期/日历的Android App崩溃

时间:2015-10-20 07:01:46

标签: android android-date

我有一个问题,当我使用Date和Calendar类时,我的应用程序崩溃,但一切都可以通过模拟器正常工作,并且不会抛出任何错误。

我需要在日期中转换rss pubDate以获取日,月和日,之后将星期日和月作为字符串。

此代码位于arrayadapter类中,并在asynk任务中调用。

public class PostItemAdapter extends ArrayAdapter<RssItem> {

static class ViewHolder {
    TextView postTitleView;
    TextView postDateView;
    ImageView postThumbView;
}

private Activity myContext;
private RssItem[] datas;

public PostItemAdapter(Context context, int textViewResourceId, RssItem[] objects) {
    super(context, textViewResourceId, objects);
    myContext = (Activity) context;
    datas = objects;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if (convertView == null)
    {
        LayoutInflater inflater = myContext.getLayoutInflater();
        convertView = inflater.inflate(R.layout.postitem, null);
        viewHolder = new ViewHolder();
        viewHolder.postThumbView = (ImageView) convertView.findViewById(R.id.postThumb);
        viewHolder.postTitleView = (TextView) convertView.findViewById(R.id.postTitleLabel);
        viewHolder.postDateView = (TextView) convertView.findViewById(R.id.postDateLabel);
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.postThumbView.setImageBitmap(datas[position].getRssThumbUrl());
    if (datas[position].getRssThumbUrl() == null)
    {
        viewHolder.postThumbView.setImageResource(R.drawable.postthumb_loading);
    }
    viewHolder.postTitleView.setText(datas[position].getRssTitle());

    String _pubDate = datas[position].getRssDate();
    DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
    Date date = null;

    try {
        date = formatter.parse(_pubDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);

    String mese ="";

    if(month == 1)
        mese = "Gennaio";
    else if(month == 2)
        mese = "Febbraio";
    else if(month == 3)
        mese = "Marzo";
    else if(month == 4)
        mese = "Aprile";
    else if(month == 5)
        mese = "Maggio";
    else if(month == 6)
        mese = "Giugno";
    else if(month == 7)
        mese = "Luglio";
    else if(month == 8)
        mese = "Agosto";
    else if(month == 9)
        mese = "Settembre";
    else if(month == 10)
        mese = "Ottobre";
    else if(month == 11)
        mese = "Novembre";
    else if(month == 12)
        mese = "Dicembre";

    int giornoDellaSettimana = cal.get(Calendar.DAY_OF_WEEK);
    String giorno="";
    if(giornoDellaSettimana == 1)
        giorno = "Domenica";
    else if(giornoDellaSettimana == 2)
        giorno = "Lunedì";
    else if(giornoDellaSettimana == 3)
        giorno = "Mertedì";
    else if(giornoDellaSettimana == 4)
        giorno = "Mercoledì";
    else if(giornoDellaSettimana == 5)
        giorno = "Giovedì";
    else if(giornoDellaSettimana == 6)
        giorno = "Venerdì";
    else if(giornoDellaSettimana == 7)
        giorno = "Sabato";

    _pubDate =day+"/"+month+"/"+year;



    viewHolder.postDateView.setText(String.valueOf(_pubDate));
    return convertView;
}

}

logcat的:

&#13;
&#13;
10-20 07:21:05.230 11709-11709/? I/art: Not late-enabling -Xcheck:jni (already on)
10-20 07:21:05.589 11709-11721/francescoperfetti.mamba.it.cosenzachannelapp_nofrag I/art: Background partial concurrent mark sweep GC freed 237(48KB) AllocSpace objects, 1(16KB) LOS objects, 50% free, 1011KB/2035KB, paused 5.284ms total 95.048ms
10-20 07:21:06.009 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag D/OpenGLRenderer: Render dirty regions requested: true
10-20 07:21:06.013 11709-11709/francescoperfetti.mamba.it.cosenzachannelapp_nofrag D/: HostConnection::get() New Host Connection established 0xa5f7a340, tid 11709
10-20 07:21:06.018 11709-11709/francescoperfetti.mamba.it.cosenzachannelapp_nofrag D/Atlas: Validating map...
10-20 07:21:06.077 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag D/: HostConnection::get() New Host Connection established 0xa5f7a890, tid 11757
10-20 07:21:06.084 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag I/OpenGLRenderer: Initialized EGL, version 1.4
10-20 07:21:06.092 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag D/OpenGLRenderer: Enabling debug mode 0
10-20 07:21:06.113 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag W/EGL_emulation: eglSurfaceAttrib not implemented
10-20 07:21:06.113 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5f51a20, error=EGL_SUCCESS
10-20 07:21:08.168 11709-11734/francescoperfetti.mamba.it.cosenzachannelapp_nofrag W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
10-20 07:21:09.331 11709-11716/francescoperfetti.mamba.it.cosenzachannelapp_nofrag W/art: Suspending all threads took: 20.517ms
10-20 07:21:09.411 11709-11721/francescoperfetti.mamba.it.cosenzachannelapp_nofrag I/art: Background partial concurrent mark sweep GC freed 3083(194KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 12MB/16MB, paused 8.301ms total 50.396ms
10-20 07:21:09.890 11709-11819/francescoperfetti.mamba.it.cosenzachannelapp_nofrag D/URL ASYNK: http://feeds.feedburner.com/Cosenzachannel
10-20 07:21:09.948 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag W/EGL_emulation: eglSurfaceAttrib not implemented
10-20 07:21:09.948 11709-11757/francescoperfetti.mamba.it.cosenzachannelapp_nofrag W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5f51a40, error=EGL_SUCCESS
10-20 07:21:12.412 11709-11716/francescoperfetti.mamba.it.cosenzachannelapp_nofrag W/art: Suspending all threads took: 97.378ms
&#13;
&#13;
&#13;

0 个答案:

没有答案