简体   繁体   English

IllegalArgumentException:解析错误java.util.Date

[英]IllegalArgumentException: Parse error java.util.Date

I'm getting low rating on the app I published recently. 我对我最近发布的应用程序评价不高。 The app crashes sometimes and users report this type of error: 该应用有时会崩溃,并且用户报告此类型的错误:

java.lang.IllegalArgumentException: Parse error: Mon, 09 May 
at java.util.Date.parseError(Date.java:364)
at java.util.Date.parse(Date.java:560)
at java.util.Date.<init>(Date.java:154)
at newproject.CustomList.getView(CustomList.java:81)
at newproject.NestedListView.onMeasure(NestedListView.java:53)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1415)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:78)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1132)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:675)
at android.view.View.measure(View.java:17637)
at android.support.v4.widget.SwipeRefreshLayout.onMeasure(SwipeRefreshLayout.java:600)
at android.view.View.measure(View.java:17637)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17637)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618)
at android.view.View.measure(View.java:17637)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
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:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)

I'm getting reported this same error pretty frequently: 我经常收到此错误的报告:

Parse error: Mon, 09 M

Parse error: Sat,

Parse error: Fri, 06 

Parse error: Thu,

Parse error: Tue, 03 May 2016 11

I really don't know how to handle this problem. 我真的不知道该如何解决这个问题。 I'm trying to display the time a news article was published. 我正在尝试显示新闻报道的发布时间。

NestedListView : NestedListView

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {

private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

public NestedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    listViewTouchAction = -1;
    setOnScrollListener(this);
    setOnTouchListener(this);
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, -1);
        }
    }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@SuppressLint("DrawAllocation")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int newHeight = 0;
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    if (heightMode != MeasureSpec.EXACTLY) {
        ListAdapter listAdapter = getAdapter();
        if (listAdapter != null && !listAdapter.isEmpty()) {
            int listPosition;
            for (listPosition = 0; listPosition < listAdapter.getCount()
                    && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                View listItem = listAdapter.getView(listPosition, null, this);
                //now it will not throw a NPE if listItem is a ViewGroup instance
                if (listItem instanceof ViewGroup) {
                    listItem.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                }
                listItem.measure(widthMeasureSpec, heightMeasureSpec);
                newHeight += listItem.getMeasuredHeight();
            }
            newHeight += getDividerHeight() * listPosition;
        }
        if ((heightMode == View.MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
            if (newHeight > heightSize) {
                newHeight = heightSize;
            }
        }
    } else {
        newHeight = getMeasuredHeight();
    }
    setMeasuredDimension(getMeasuredWidth(), newHeight);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
        if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
            scrollBy(0, 1);
        }
    }
    return false;
}

This line is indicated in the log: View listItem = listAdapter.getView(listPosition, null, this); 此行在日志中指示: View listItem = listAdapter.getView(listPosition,null,this); (line 53) (第53行)

I'm doing this way because, in my opinion, the app would look more beautiful if there is a "Yesterday" instead of "Thu, 12 May 2016 15:43:00 +0200"; 我这样做是因为,如果我用的是“昨天”而不是“ 2016年5月12日,星期四15:43:00 +0200”,则该应用看上去会更漂亮。 or "2 days ago", ... and so on. 或“ 2天前”等等。

CustomList : CustomList

public class CustomList extends ArrayAdapter<RSSItem> {

private static Activity context = null;
private final List<RSSItem> web;

public CustomList(Activity context, List<RSSItem> web) {
    super(context, R.layout.new_listview, web);
    CustomList.context = context;
    this.web = web;
}

@SuppressLint("SetTextI18n")
@Override
public View getView(final int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    @SuppressLint({"ViewHolder", "InflateParams"}) final View rowView = inflater.inflate(R.layout.new_listview, null, true);

    ImageView imageView = (ImageView)rowView.findViewById(R.id.image);
    Picasso.with(context).load(web.get(position).getImage()).into(imageView);

    TextView textView = (TextView)rowView.findViewById(R.id.title);
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "RobotoSlab-Regular.ttf");
    textView.setTypeface(typeface);
    textView.setText(Html.fromHtml(web.get(position).getTitle()));

    TextView textView2 = (TextView)rowView.findViewById(R.id.pubdate);

    /** Months **/

    @SuppressLint("SimpleDateFormat") DateFormat df_month = new SimpleDateFormat("MM");
    Date date_month = new Date(web.get(position).getPubdate());
    String pubdate_month = df_month.format(date_month);

    Calendar cal_month = Calendar.getInstance();
    @SuppressLint("SimpleDateFormat") DateFormat month_date = new SimpleDateFormat("MM");
    String month_name = month_date.format(cal_month.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_p_month = new SimpleDateFormat("MM");
    Calendar cal_p_month = Calendar.getInstance();
    cal_p_month.add(Calendar.MONTH, -1);
    String date_p_month = df_p_month.format(cal_p_month.getTime());


    /** Days **/

    @SuppressLint("SimpleDateFormat") DateFormat df_day = new SimpleDateFormat("dd");
    Date date_day = new Date((web.get(position).getPubdate());
    String pubdate_day = df_day.format(date_day);

    @SuppressLint("SimpleDateFormat") DateFormat df_0 = new SimpleDateFormat("dd");
    Calendar cal_0 = Calendar.getInstance();
    cal_0.add(Calendar.DAY_OF_YEAR, -0);
    String date_0 = df_0.format(cal_0.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_1 = new SimpleDateFormat("dd");
    Calendar cal_1 = Calendar.getInstance();
    cal_1.add(Calendar.DAY_OF_YEAR, -1);
    String date_1 = df_1.format(cal_1.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_2 = new SimpleDateFormat("dd");
    Calendar cal_2 = Calendar.getInstance();
    cal_2.add(Calendar.DAY_OF_YEAR, -2);
    String date_2 = df_2.format(cal_2.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_3 = new SimpleDateFormat("dd");
    Calendar cal_3 = Calendar.getInstance();
    cal_3.add(Calendar.DAY_OF_YEAR, -3);
    String date_3 = df_3.format(cal_3.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_4 = new SimpleDateFormat("dd");
    Calendar cal_4 = Calendar.getInstance();
    cal_4.add(Calendar.DAY_OF_YEAR, -4);
    String date_4 = df_4.format(cal_4.getTime());

    @SuppressLint("SimpleDateFormat") DateFormat df_5 = new SimpleDateFormat("dd");
    Calendar cal_5 = Calendar.getInstance();
    cal_5.add(Calendar.DAY_OF_YEAR, -5);
    String date_5 = df_5.format(cal_5.getTime());


    if(pubdate_month.equalsIgnoreCase(month_name)){

        if(pubdate_day.equalsIgnoreCase(date_0)){
            @SuppressLint("SimpleDateFormat") DateFormat df = new SimpleDateFormat("HH:mm");
            Date date = new Date(String.valueOf(Html.fromHtml(web.get(position).getPubdate())));
            String pubdate = df.format(date);
            textView2.setText(pubdate);
        }else if(pubdate_day.equalsIgnoreCase(date_1)){
            textView2.setText("Yesterday");
        }else if(pubdate_day.equalsIgnoreCase(date_2)){
            textView2.setText("2 days ago");
        }else if(pubdate_day.equalsIgnoreCase(date_3)){
            textView2.setText("3 days ago");
        }else if(pubdate_day.equalsIgnoreCase(date_4)){
            textView2.setText("4 days ago");
        }else if(pubdate_day.equalsIgnoreCase(date_5)){
            textView2.setText("5 days ago");
        }else if(pubdate_month.equalsIgnoreCase(date_p_month)){
            textView2.setText("1 month ago");
        }

    } else {
        textView2.setText("Today");
    }

    return rowView;

}

This line is indicated in the log: _Date date_month = new Date(web.get(position).getPubdate());_ (line 81) 该行在日志中指示:_Date date_month = new Date(web.get(position).getPubdate()); _(第81行)

Could you help me please? 请问你能帮帮我吗?

Edit 编辑

TextView textView2 = (TextView)rowView.findViewById(R.id.pubdate);

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy h:mm:ss Z");
    try {
        Date date = sdf.parse(web.get(position).getPubdate());

        /** Months **/

        DateFormat df_month = new SimpleDateFormat("MM");
        String pubdate_month = df_month.format(date);

        Calendar cal_month = Calendar.getInstance();
        DateFormat month_date = new SimpleDateFormat("MM");
        String month_name = month_date.format(cal_month.getTime());

        DateFormat df_p_month = new SimpleDateFormat("MM");
        Calendar cal_p_month = Calendar.getInstance();
        cal_p_month.add(Calendar.MONTH, -1);
        String date_p_month = df_p_month.format(cal_p_month.getTime());

        /** Days **/

        DateFormat df_day = new SimpleDateFormat("dd");
        String pubdate_day = df_day.format(date);

        DateFormat df_0 = new SimpleDateFormat("dd");
        Calendar cal_0 = Calendar.getInstance();
        cal_0.add(Calendar.DAY_OF_YEAR, -0);
        String date_0 = df_0.format(cal_0.getTime());

        DateFormat df_1 = new SimpleDateFormat("dd");
        Calendar cal_1 = Calendar.getInstance();
        cal_1.add(Calendar.DAY_OF_YEAR, -1);
        String date_1 = df_1.format(cal_1.getTime());

        DateFormat df_2 = new SimpleDateFormat("dd");
        Calendar cal_2 = Calendar.getInstance();
        cal_2.add(Calendar.DAY_OF_YEAR, -2);
        String date_2 = df_2.format(cal_2.getTime());

        DateFormat df_3 = new SimpleDateFormat("dd");
        Calendar cal_3 = Calendar.getInstance();
        cal_3.add(Calendar.DAY_OF_YEAR, -3);
        String date_3 = df_3.format(cal_3.getTime());

        DateFormat df_4 = new SimpleDateFormat("dd");
        Calendar cal_4 = Calendar.getInstance();
        cal_4.add(Calendar.DAY_OF_YEAR, -4);
        String date_4 = df_4.format(cal_4.getTime());

        DateFormat df_5 = new SimpleDateFormat("dd");
        Calendar cal_5 = Calendar.getInstance();
        cal_5.add(Calendar.DAY_OF_YEAR, -5);
        String date_5 = df_5.format(cal_5.getTime());

        if(pubdate_month.equalsIgnoreCase(month_name)){
            if(pubdate_day.equalsIgnoreCase(date_0)){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                String date1 = simpleDateFormat.parse(web.get(position).getPubdate()).toString();
                textView2.setText(date1);
            }else if(pubdate_day.equalsIgnoreCase(date_1)){
                textView2.setText("Yesterday");
            }else if(pubdate_day.equalsIgnoreCase(date_2)){
                textView2.setText("2 days ago");
            }else if(pubdate_day.equalsIgnoreCase(date_3)){
                textView2.setText("3 days ago");
            }else if(pubdate_day.equalsIgnoreCase(date_4)){
                textView2.setText("4 days ago");
            }else if(pubdate_day.equalsIgnoreCase(date_5)){
                textView2.setText("5 days ago");
            }else if(pubdate_day.isEmpty()){
                textView2.setText("Today");
            }else if(pubdate_month.isEmpty()){
                textView2.setText("1 week ago");
            }else if(pubdate_month.equalsIgnoreCase(date_p_month)){
                textView2.setText("1 month ago");
            }
        } else {
            textView2.setText("Today");
        }

    } catch (ParseException e) {
        e.printStackTrace();
    }

What is the returned type of getPubdate() method? getPubdate()方法的返回类型是什么? Is it long or String? 是长还是字符串?

Check if it's not a null and is long. 检查它是否不是null且很长。

Update: 更新:

Since the returned date from web.get(position).getPubdate() is in a String format, you have to parse it to a Date object via SimpleDateFormat, ie: 由于从web.get(position).getPubdate()返回的日期为String格式,因此您必须通过SimpleDateFormat将其解析为Date对象,即:

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy h:mm:ss Z");
Date date = sdf.parse(web.get(position).getPubdate());

You are calling the new Date(String s) constructor. 您正在调用new Date(String s)构造函数。 Quoting javadoc: 引用javadoc:

Deprecated. 已过时。 As of JDK version 1.1 , replaced by DateFormat.parse(String s) . 从JDK 1.1版开始 ,由DateFormat.parse(String s)取代。

Allocates a Date object and initializes it so that it represents the date and time indicated by the string s , which is interpreted as if by the parse(java.lang.String) method. 分配一个Date对象并对其进行初始化,以便它表示由字符串s指示的日期和时间,就像由parse(java.lang.String)方法解释那样。

You are using a method that have been deprecated since 1997 . 您使用的是自1997年以来不推荐使用的方法。 Deprecated means "that programmers are discouraged from using it, typically because it is dangerous, or because a better alternative exists ". 不推荐使用是指“不鼓励程序员使用它,通常是因为这样做很危险,或者因为存在更好的选择 ”。

The error message says that getPubdate() returned the string Mon, 09 May . 错误消息指出getPubdate()返回字符串Mon, 09 May This definitely not a value that any Java parser can handle without an explicit format string. 如果没有显式格式字符串,这绝对不是任何Java解析器都可以处理的值。 This includes the new Date(String s) call that you shouldn't be using anyway. 这包括您不应该使用的new Date(String s)调用。

What kind of date value is that even supposed to be? 那应该算是什么样的日期值? There is no year! 没有年份!

Looking at the top of the stacktrace it looks like an incomplete string is being passed to the Date constructor, ie, there is no year and it's possibly illformed. 从堆栈跟踪的顶部看,好像有一个不完整的字符串正在传递给Date构造函数,即没有年份,并且可能格式错误。

Try capturing what web.get(position).getPubdate() returns before calling the Date constructor. 在调用Date构造函数之前,请尝试捕获web.get(position).getPubdate()返回的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM