簡體   English   中英

IllegalArgumentException:解析錯誤java.util.Date

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

我對我最近發布的應用程序評價不高。 該應用有時會崩潰,並且用戶報告此類型的錯誤:

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)

我經常收到此錯誤的報告:

Parse error: Mon, 09 M

Parse error: Sat,

Parse error: Fri, 06 

Parse error: Thu,

Parse error: Tue, 03 May 2016 11

我真的不知道該如何解決這個問題。 我正在嘗試顯示新聞報道的發布時間。

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;
}

此行在日志中指示: View listItem = listAdapter.getView(listPosition,null,this); (第53行)

我這樣做是因為,如果我用的是“昨天”而不是“ 2016年5月12日,星期四15:43:00 +0200”,則該應用看上去會更漂亮。 或“ 2天前”等等。

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;

}

該行在日志中指示:_Date date_month = new Date(web.get(position).getPubdate()); _(第81行)

請問你能幫幫我嗎?

編輯

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();
    }

getPubdate()方法的返回類型是什么? 是長還是字符串?

檢查它是否不是null且很長。

更新:

由於從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());

您正在調用new Date(String s)構造函數。 引用javadoc:

已過時。 從JDK 1.1版開始 ,由DateFormat.parse(String s)取代。

分配一個Date對象並對其進行初始化,以便它表示由字符串s指示的日期和時間,就像由parse(java.lang.String)方法解釋那樣。

您使用的是自1997年以來不推薦使用的方法。 不推薦使用是指“不鼓勵程序員使用它,通常是因為這樣做很危險,或者因為存在更好的選擇 ”。

錯誤消息指出getPubdate()返回字符串Mon, 09 May 如果沒有顯式格式字符串,這絕對不是任何Java解析器都可以處理的值。 這包括您不應該使用的new Date(String s)調用。

那應該算是什么樣的日期值? 沒有年份!

從堆棧跟蹤的頂部看,好像有一個不完整的字符串正在傳遞給Date構造函數,即沒有年份,並且可能格式錯誤。

在調用Date構造函數之前,請嘗試捕獲web.get(position).getPubdate()返回的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM