应用程序崩溃,与日期差异有关



什么会导致这个崩溃发生,不知道在哪里看,我已经从我认为错误来自以及它抛出的错误的地方附加了代码,应用程序工作正常,只是当我去一个特定的部分它崩溃与这个错误。

D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 4
W/System.err: java.text.ParseException: Unparseable date: "null"
at java.text.DateFormat.parse(DateFormat.java:362)
at com.buzzr.ca.skeloo.utils.DateUtils.getDiff(DateUtils.java:56)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity.setupOfferDate(OfferDetailActivity.java:509)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity.setupComponents(OfferDetailActivity.java:460)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity.access$200(OfferDetailActivity.java:76)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity$4.onResponse(OfferDetailActivity.java:606)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity$4.onResponse(OfferDetailActivity.java:586)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
W/System.err:     at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/oldDate: -62075467800000 - 0002-11-30 00:00:00
E/currentDate: 1629102262245 - 0002-11-30 00:00:00
E/Difference::  seconds: -63704570062 minutes: -1061742834 hours: -17695713 days: -737321
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.buzzr.ca.skeloo, PID: 11310
java.lang.StringIndexOutOfBoundsException: length=0; index=0
at java.lang.String.charAt(Native Method)
at android.graphics.Color.parseColor(Color.java:1384)
at com.buzzr.ca.skeloo.customView.StoreCardCustomView.updateCategoryBadge(StoreCardCustomView.java:280)
at com.buzzr.ca.skeloo.customView.StoreCardCustomView.setupComponent(StoreCardCustomView.java:263)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity.setupComponents(OfferDetailActivity.java:465)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity.access$200(OfferDetailActivity.java:76)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity$4.onResponse(OfferDetailActivity.java:606)
at com.buzzr.ca.skeloo.activities.OfferDetailActivity$4.onResponse(OfferDetailActivity.java:586)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/buzzr.ca.skelo: Background young concurrent copying GC freed 143074(4393KB) AllocSpace objects, 6(268KB) LOS objects, 34% free, 8500KB/12MB, paused 1.171ms total 146.600ms
I/Process: Sending signal. PID: 11310 SIG: 9

我真的不知道去哪里看,但我想这与日期的差异有关。如果有任何帮助,我将不胜感激。

这里是OfferDetailActivity.java

/*
*
*   DATE & COUNTDOWN
*
*/
String date = "";

try {
date = mOffer.getDate_start();
date = DateUtils.prepareOutputDate(date, "dd MMMM yyyy  hh:mm", this);
} catch (Exception e) {
syncOffer(offer_id);
return;
}
}

private void setupComponents(final Offer mOffer) {
offerData = mOffer;
getAppBarTitle().setText(mOffer.getName());
header_title.setText(mOffer.getName());

if (mOffer.getValue_type().equalsIgnoreCase("Percent") && (mOffer.getOffer_value() > 0 || mOffer.getOffer_value() < 0)) {
DecimalFormat decimalFormat = new DecimalFormat("#0");
badge_price.setText(decimalFormat.format(mOffer.getOffer_value()) + "%");
} else {
if (mOffer.getValue_type().equalsIgnoreCase("Price") && mOffer.getOffer_value() != 0) {
badge_price.setText(OfferUtils.parseCurrencyFormat(
mOffer.getOffer_value(),
mOffer.getCurrency()));
} else {
badge_price.setText(getString(R.string.promo));
}
}
badge_price.setVisibility(View.VISIBLE);
if (mOffer.getImages() != null)
Glide.with(getBaseContext()).load(mOffer.getImages().getUrl500_500())
.centerCrop()
.placeholder(ImageLoaderAnimation.glideLoader(this))
.into(image);

description_content.setText(mOffer.getDescription());
new TextUtils.decodeHtml(description_content).execute(mOffer.getDescription());
Textoo
.config(description_content)
.linkifyWebUrls()  // or just .linkifyAll()
.addLinksHandler(new LinksHandler() {
@Override
public boolean onClick(View view, String url) {
if (Utils.isValidURL(url)) {
new AwesomeWebView.Builder(OfferDetailActivity.this)
.showMenuOpenWith(false)
.statusBarColorRes(R.color.colorAccent)
.theme(R.style.FinestWebViewAppTheme)
.titleColor(
ResourcesCompat.getColor(getResources(), R.color.white, null)
).urlColor(
ResourcesCompat.getColor(getResources(), R.color.white, null)
).show(url);
return true;
} else {
return false;
}
}
})
.apply();
try {
int cid = Integer.parseInt(getIntent().getExtras().getString("cid"));
CampagneController.markView(cid);
// Toast.makeText(this,"CMarkViewClicked",Toast.LENGTH_LONG).show();
} catch (Exception e) {
if (AppConfig.APP_DEBUG)
e.printStackTrace();
}
//setup offer date
setupOfferDate(mOffer);

if (mOffer.getStore_id() > 0 && StoreController.getStore(mOffer.getStore_id()) != null) {
Store storeOffers = StoreController.getStore(mOffer.getStore_id());
customStoreCV.setupComponent(storeOffers);
updateCategoryBadge(storeOffers.getCategory_name(), storeOffers.getCategory_color());
} else {
customStoreCV.loadData(mOffer.getStore_id(), false, new StoreCardCustomView.StoreListener() {
@Override
public void onLoaded(Store object) {
updateCategoryBadge(object.getCategory_name(), object.getCategory_color());
}
});
}

}
@SuppressLint("StringFormatMatches")
private void setupOfferDate(Offer mOffer) {
String dateStartAt = "";
String dateEndAt = "";
try {
dateStartAt = mOffer.getDate_start();
dateStartAt = DateUtils.prepareOutputDate(dateStartAt, "dd MMMM yyyy", this);
} catch (Exception e) {
return;
}
try {
dateEndAt = mOffer.getDate_end();
dateEndAt = DateUtils.prepareOutputDate(dateEndAt, "dd MMMM yyyy", this);
} catch (Exception e) {
return;
}
//        if(mOffer.getType()==0){
//            CountdownView mCvCountdownView = (CountdownView)findViewById(R.id.cv_countdownViewTest1);
//            mCvCountdownView.setVisibility(View.GONE);
//            typeView.setVisibility(View.GONE);
//        }else {
String inputDateSatrt = DateUtils.prepareOutputDate(mOffer.getDate_start(), "yyyy-MM-dd HH:mm:ss", this);
long diff_Will_Start = DateUtils.getDiff(inputDateSatrt, "yyyy-MM-dd HH:mm:ss");
if (AppConfig.APP_DEBUG) {
Log.e("_start_at_server", mOffer.getDate_start());
Log.e("_start_at_device ", dateStartAt);
Log.e("_start_at_diff ", String.valueOf(diff_Will_Start));
}

if (diff_Will_Start > 0) {
offer_up_to.setText(String.format(getString(R.string.offer_start_at), dateStartAt));
if (dateStartAt != null && dateStartAt.equals("null")) {
offer_layout.setVisibility(View.GONE);
}
}

String inputDateEnd = DateUtils.prepareOutputDate(mOffer.getDate_end(), "yyyy-MM-dd HH:mm:ss", this);
long diff_will_end = DateUtils.getDiff(inputDateEnd, "yyyy-MM-dd HH:mm:ss");

if (AppConfig.APP_DEBUG) {
Log.e("_end_at_server", mOffer.getDate_end());
Log.e("_end_at_device ", dateEndAt);
Log.e("_end_at_diff ", String.valueOf(diff_will_end));
}

if (diff_will_end > 0 && diff_Will_Start < 0) {
if (dateEndAt == null || dateEndAt.equals("null")) {
offer_layout.setVisibility(View.GONE);
} else {
offer_up_to.setText(String.format(getString(R.string.offer_end_at), dateEndAt));
offer_layout.setVisibility(View.VISIBLE);
}
}

if (diff_Will_Start < 0 && diff_will_end < 0) {
offer_layout.setVisibility(View.VISIBLE);
offer_up_to.setText(String.format(getString(R.string.offer_ended_at), dateEndAt));
}

这是dateutil .java

package com.buzzr.ca.skeloo.utils;
import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;
import com.buzzr.ca.skeloo.AppController;
import com.buzzr.ca.skeloo.appconfig.AppConfig;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/**
* Created by Droideve on 8/17/2016.
*/
public class DateUtils {

public static String getCurrentDay() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
return new SimpleDateFormat("EE", Locale.ENGLISH).format(date.getTime()).toLowerCase();
}

public static String getPrepareSimpleDate(String inputDate, String outputSchema) {
SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputSchema, Locale.ENGLISH);
try {
Date date = inputFormat.parse(inputDate);
return outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return "N/A";
}

public static Long getDiff(String toyBornTime, String schema) {
SimpleDateFormat dateFormat = new SimpleDateFormat(schema);
try {
Date oldDate = dateFormat.parse(toyBornTime);
Date currentDate = new Date();
long diff = oldDate.getTime() - currentDate.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
Log.e("oldDate", "" + oldDate.getTime() + " - " + dateFormat.format(oldDate.getTime()));
Log.e("currentDate", "" + currentDate.getTime() + " - " + dateFormat.format(oldDate.getTime()));
Log.e("Difference: ", " seconds: " + seconds + " minutes: " + minutes
+ " hours: " + hours + " days: " + days);
// Log.e("toyBornTime", "" + toyBornTime);
return diff;
} catch (ParseException e) {
e.printStackTrace();
}
return Long.valueOf(0);
}

public static String getUTC(String schema) {
Date myDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTime(myDate);
Date time = calendar.getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.ENGLISH);
String dateAsString = outputFmt.format(time);
return dateAsString;
}

public static String getLocalTime(String schema) {
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat outputFmt = new SimpleDateFormat(schema);
return outputFmt.format(todayDate);
}

public static String getDateByTimeZone(String dateStr, String schema) {
String inputPattern = "yyyy-MM-dd";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
inputFormat.setTimeZone(TimeZone.getDefault());
Locale current = AppController.getInstance().getResources().getConfiguration().locale;
try {
Date inputDate = inputFormat.parse(dateStr);
SimpleDateFormat formatter = null;
if (schema != null) {
if (AppConfig.APP_DEBUG)
Log.e("dateUtilsSchema", schema + " - " + current);
formatter = new SimpleDateFormat(schema, current);
} else {
formatter = new SimpleDateFormat("dd MMMM yyyy hh:mm", current);
}
formatter.setTimeZone(TimeZone.getDefault());
return formatter.format(inputDate);
} catch (Exception e) {
e.printStackTrace();
return dateStr;
}
}

public static String prepareOutputDate(String dateStr, String schema, Context context) {
String inputPattern = "yyyy-MM-dd hh:mm";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
try {
inputFormat.setTimeZone(TimeZone.getDefault());
Date inputDate = inputFormat.parse(dateStr);
String hourFormat = "hh:mm";
if (context != null) {
if (hourFormat12(context)) {
hourFormat = "hh:mm";
} else {
hourFormat = "kk:mm";
}
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy " + hourFormat);
SimpleDateFormat formatterHour = new SimpleDateFormat(hourFormat);
if (schema != null) {
formatter = new SimpleDateFormat(schema);
formatterHour = new SimpleDateFormat(schema);
}

formatter.setTimeZone(TimeZone.getDefault());
formatterHour.setTimeZone(TimeZone.getDefault());
int diffrence = minutesDifference(dateStr);
if (diffrence < 1440) {
return formatterHour.format(inputDate);
} else {
return formatter.format(inputDate);
}

} catch (ParseException e) {

return dateStr;
}
}

public static boolean isLessThan24(String dateString, String format) {
try {
SimpleDateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd H:m:s");
inputFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dateBegin = inputFormatter.parse(dateString);
Date dateCurrent = new Date();

long result = dateBegin.getTime() - dateCurrent.getTime();
long days = TimeUnit.DAYS.convert(result, TimeUnit.MILLISECONDS);
long hours = TimeUnit.HOURS.convert(result, TimeUnit.MILLISECONDS);
if (AppConfig.APP_DEBUG) {
Log.e("upcoming tz: ", TimeZone.getDefault().getDisplayName());
Log.e("upcoming date: " + dateBegin + " days:", days + " " + result);
Log.e("upcoming date: " + dateBegin + " hours:", hours + " " + result);
Log.e("upcoming", "==================  ===================");
}
if (hours < 24 && hours >= 0) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
public static int minutesDifference(String dateStr) {
int MILLI_TO_MINUTE = 1000 * 60;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd H:m:s");
Date currentDate = new Date();
try {
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(dateStr);
formatter.setTimeZone(TimeZone.getDefault());
String newDate = formatter.format(date);
date = formatter.parse(newDate);

return (int) (currentDate.getTime() - date.getTime()) / MILLI_TO_MINUTE;
} catch (ParseException e) {
e.printStackTrace();
}

return 0;
}

private static boolean hourFormat12(Context context) {
Calendar mCalendar = null;
return !DateFormat.is24HourFormat(context);
}
}

感谢

让我们来分析一下。

首先,你的错误日志显示你需要查看的地方:

W/System.err: java.text.ParseException: Unparseable date: "null"
at java.text.DateFormat.parse(DateFormat.java:362)
at com.buzzr.ca.skeloo.utils.DateUtils.getDiff(DateUtils.java:56)

所以在你的DateUtils类的第56行,当你调用dateFormat。解析string中的过去的值为null(因此可以被解析)。

现在我们可以回顾一下你的代码,找出为什么它有这个值。

在你的OfferDetailActivity类的行是问题是

String inputDateSatrt = DateUtils.prepareOutputDate(mOffer.getDate_start(), "yyyy-MM-dd HH:mm:ss", this);
long diff_Will_Start = DateUtils.getDiff(inputDateSatrt, "yyyy-MM-dd HH:mm:ss");

使得inputDataSatrt的值为"null"出于某种原因,让我们看看prepareOutputDate方法来找出原因。在这一点上,它也值得看看你的尝试块…为什么你试图设置字符串dateStartAt,但不使用它的值?当您可以使用dateStartAt值而不必再次调用prepareOutputDate时,这里存在代码重复。

让我们看看prepareOutputDate在你的dateutil。

往回看,看看我们是否能找到为什么返回的字符串值是"null"。如果不知道Offer类包含什么或mOffer.getDate_end()返回什么,这将更难,但我们可以假设它确实返回。

你的类,如果它命中一个ParseException(因为例如,dateStr的值不是在正确的格式将返回dateStr的值…期望不会被抛出,你的try block会继续。然而,在getDiff类中,您打印Exception给出您看到的错误。

如何处理解析异常是不一致的,因此可以安全地假设mOffer.getDate_start()返回"null">

最新更新