2 u010321179 u010321179 于 2013.04.17 13:20 提问

20130310字符串转换

想要转换字符串日期=20130310到日期格式。

在安卓中运行之后给我报出了异常。并且在滚动时就崩溃。

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            Date parsed = format.parse(pendingTrade.voteDeadline);
            System.out.println(" 2. " + NEWS_DATE_FORMAT.format(parsed));
            holder.deadline.setText("Deadline | " + NEWS_DATE_FORMAT.format(parsed));
        }
        catch(ParseException pe) {
            System.out.println("ERROR: Cannot parse \"" + pendingTrade.voteDeadline + "\"");

日志:

04-16 13:54:36.410: E/AndroidRuntime(13885): java.lang.NullPointerException
04-16 13:54:36.410: E/AndroidRuntime(13885):    at     java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1001)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at java.text.DateFormat.parse(DateFormat.java:620)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at com.cbssports.nflapp.ffb.ui.FragmentPendingTrades$PendingTradesAdapter.getView(FragmentPendingTrades.java:1443)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.AbsListView.obtainView(AbsListView.java:2143)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.ListView.fillDown(ListView.java:674)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.ListView.fillGap(ListView.java:638)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4087)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer.doFrame(Choreographer.java:531)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.os.Handler.handleCallback(Handler.java:725)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.os.Looper.loop(Looper.java:137)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 13:54:36.410: E/AndroidRuntime(13885):    at dalvik.system.NativeStart.main(Native Method)

2个回答

Kill_it
Kill_it   2013.04.17 14:13
已采纳

你获得的异常是NullPointerException
检查一下pendingTrade.voteDeadline的值,可能是空值引起的。

u010321179
u010321179 是的我检查了一下是空值。谢谢
接近 5 年之前 回复
luhuajcdd
luhuajcdd   2013.04.17 14:01

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd") ;
String s = format.format(new Date()) ;
在android中这样是没有错的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!