减去两次后,TextView上没有显示任何内容(hh:mm:ss)

时间:2013-09-22 01:26:54

标签: java android string jsoup

我正在开发一款Android应用。我将2个日期的差异设置为TextView。它没有显示任何内容。在某个地方,它没有到达TextView。如果你能告诉我哪里出错了,我会很高兴。我的代码如下。现在,在添加新代码后,它会强行关闭。使用新代码更新代码

TestStation.java

public class TestStation extends Activity {
String URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.test_station);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 



Document doc = null;

TextView tv = (TextView) findViewById(R.id.tv);

try {
    doc = Jsoup.connect(URL).userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10").get();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Elements elem = doc.select("eta");
for (Element div : elem) {

}Elements elemn = doc.select("eta"); for (Element div : elem) {
Elements arrT = div.select("arrT");
Elements prdt = div.select("prdt");

String value = arrT.val(); 
String valu = prdt.val();

Date date1 = new Date(valu);
Date date2 = new Date(value);

long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:00");
sdf.format(dateDiff);
tv.setText(String.valueOf (dateDiff));

}

}

LogCat显示大量红色(错误):

09-22 12:39:38.348: E/AndroidRuntime(9713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.chicagotraintracker/com.dev.chicagotraintracker.TestStation}:     java.lang.IllegalArgumentException: Parse error: 
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.os.Looper.loop(Looper.java:137)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.lang.reflect.Method.invoke(Method.java:525)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at dalvik.system.NativeStart.main(Native Method)
09-22 12:39:38.348: E/AndroidRuntime(9713): Caused by: java.lang.IllegalArgumentException: Parse error: 
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.parseError(Date.java:367)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.parse(Date.java:563)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.<init>(Date.java:156)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.dev.chicagotraintracker.TestStation.onCreate(TestStation.java:53)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.Activity.performCreate(Activity.java:5133)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

问题不在于TextView

主要问题是日期解析

你没有捕获第一个解析异常,它可能没有正确的格式被捕获 检查 vlau

     try{
        Date date = sdf.parse(value);
        try
        {
            Date dat = sdf.parse(valu);
            long diff = dat.getTime() - date.getTime();
            tv.setText(String.valueOf(diff));
        }
        catch(ParseException f)
        {
            f.printStackTrace();
        }
        finally
        {}
    }
    catch(ParseException f)
    {
        f.printStackTrace();
    }
    finally
    {}

编辑1:

您可以通过这种方式获取DateDifference

Date date1 = new SimpleDateFormat("GiveYourFormate", Locale.ENGLISH).parse(stringDate1);
Date date2 = new SimpleDateFormat("GiveYourFormate", Locale.ENGLISH).parse(stringDate2);
long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:00");
sdf.format(dateDiff);