在我最近发布的应用程序上,我的评价很低。应用程序有时会崩溃,用户会报告这种错误:
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+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 =新日期(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();
}发布于 2016-05-12 20:38:08
返回的getPubdate()方法类型是什么?是长的还是串的?
检查它是否为空且是长的。
更新:
由于web.get(position).getPubdate()返回的日期为字符串格式,因此必须通过SimpleDateFormat将其解析为date对象,即:
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy h:mm:ss Z");
Date date = sdf.parse(web.get(position).getPubdate());发布于 2016-05-12 20:41:31
您正在调用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)调用。
那应该是什么样的日期值?没有一年了!
发布于 2016-05-12 20:40:57
从堆栈跟踪的顶部看,似乎有一个不完整的字符串正在传递给日期构造函数,也就是说,没有年份,而且可能是错误的。
在调用日期构造函数之前,尝试捕获web.get(position).getPubdate()返回的内容。
https://stackoverflow.com/questions/37196886
复制相似问题