为什么这个简单的字符串格式现在抛出异常?

时间:2017-06-22 14:53:42

标签: vb.net visual-studio

我正在使用Authorize.net API,他们需要将卡到期字段格式化为“yyyy-mm”。我们用这个简单的代码行完成了这个:

06-22 17:33:17.073 1414-1600/? E/AudioFlinger: not enough memory for AudioTrack size=131296 06-22 17:33:17.073 1414-1600/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 06-22 17:33:17.073 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12 06-22 17:33:17.074 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 06-22 17:33:17.074 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 06-22 17:33:22.111 1414-1599/? E/AudioFlinger: not enough memory for AudioTrack size=131296 06-22 17:33:22.111 1414-1599/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 06-22 17:33:22.111 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12 06-22 17:33:22.120 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 06-22 17:33:22.120 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 06-22 17:33:26.330 1517-1530/? E/memtrack: Couldn't load memtrack module 06-22 17:33:27.159 1414-1499/? E/AudioFlinger: not enough memory for AudioTrack size=131296 06-22 17:33:27.159 1414-1499/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 06-22 17:33:27.159 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12 06-22 17:33:27.165 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 06-22 17:33:27.166 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 06-22 17:33:32.203 1414-1499/? E/AudioFlinger: not enough memory for AudioTrack size=131296 06-22 17:33:32.203 1414-1499/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 06-22 17:33:32.203 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12 06-22 17:33:32.211 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 06-22 17:33:32.211 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 06-22 17:33:37.259 1414-1599/? E/AudioFlinger: not enough memory for AudioTrack size=131296 06-22 17:33:37.259 1414-1599/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 06-22 17:33:37.260 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12 06-22 17:33:37.265 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 06-22 17:33:37.265 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 06-22 17:33:42.359 1414-1414/? E/AudioFlinger: not enough memory for AudioTrack size=131296 06-22 17:33:42.360 1414-1414/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 06-22 17:33:42.360 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12 06-22 17:33:42.366 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 06-22 17:33:42.368 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 06-22 17:33:47.604 1414-1599/? E/AudioFlinger: not enough memory for AudioTrack size=131296 06-22 17:33:47.606 1414-1599/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block? 06-22 17:33:47.652 2269-10242/? E/AudioRecord: AudioFlinger could not create record track, status: -12 06-22 17:33:47.824 2269-10242/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12. 06-22 17:33:47.825 2269-10242/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object. 06-22 17:33:47.835 2269-10242/? E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded 06-22 17:33:48.125 1517-1530/? E/memtrack: Couldn't load memtrack module 06-22 17:33:48.760 1417-1573/? E/installd: Failed to delete /data/app/vmdl1116498233.tmp: No such file or directory 06-22 17:33:48.930 2154-4609/? E/NetworkScheduler.SR: Invalid parameter app 06-22 17:33:48.930 2154-4609/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras? 06-22 17:33:49.162 2656-2656/? E/Finsky: [2] com.google.android.finsky.wear.bl.a(3): onConnectionFailed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null} 06-22 17:33:49.272 2154-4609/? E/NetworkScheduler.SR: Invalid parameter app 06-22 17:33:49.272 2154-4609/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras? 06-22 17:33:49.547 1342-1342/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 130 ms 06-22 17:33:49.752 2269-9548/? E/ContentStoreEUAS: Failed to commit the deferred actions 06-22 17:33:51.037 1352-1368/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property 06-22 17:33:51.525 2154-4609/? E/ctxmgr: [AppIntervalImpl]closeInterval: ongoing 06-22 17:33:51.545 2154-2383/? E/UidImportanceUtils: exception when invoking removeOnUidImportanceListener java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at alvy.a(:com.google.android.gms:1123) at akva.a(:com.google.android.gms:44820) at lsx.a(:com.google.android.gms:246) at akuz.run(:com.google.android.gms:2812) at android.os.Handler.handleCallback(Handler.java:769) at android.os.Handler.dispatchMessage(Handler.java:98) at alxb.dispatchMessage(:com.google.android.gms:3028) at android.os.Looper.loop(Looper.java:164) at android.os.HandlerThread.run(HandlerThread.java:65) Caused by: java.lang.IllegalArgumentException: Listener not registered: alvz@2c93e02 at android.app.ActivityManager.removeOnUidImportanceListener(ActivityManager.java:3628) at java.lang.reflect.Method.invoke(Native Method)  at alvy.a(:com.google.android.gms:1123)  at akva.a(:com.google.android.gms:44820)  at lsx.a(:com.google.android.gms:246)  at akuz.run(:com.google.android.gms:2812)  at android.os.Handler.handleCallback(Handler.java:769)  at android.os.Handler.dispatchMessage(Handler.java:98)  at alxb.dispatchMessage(:com.google.android.gms:3028)  at android.os.Looper.loop(Looper.java:164)  at android.os.HandlerThread.run(HandlerThread.java:65)  06-22 17:33:53.354 1517-1608/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?). 06-22 17:34:00.005 1517-1530/? E/memtrack: Couldn't load memtrack module

这绝对有效。我仍然使用此方法保存在系统中的卡片!但今天我正在测试一些完全无关的东西,想要添加另一张卡,而且这个代码爆炸了这个例外:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.group6.travlhoe"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

那个例外是:

expirationDate = model.Year.ToString("D4") & "-" & model.Month.ToString("D2")

这只是......对我没有意义。为什么在世界上它试图将格式说明符(D4)转换为整数?什么输入字符串?两天内世界变化了什么?

2 个答案:

答案 0 :(得分:1)

问题是您使用的是Nullable(Of Integer)。这是一种不同的结构,不支持普通ToString所具有的Integer方法的重载。

您可以查看Nullable结构here的重载。

我建议您使用GetValueOrDefault()方法获取正确的Integer,并在价值为Nothing的情况下应用您期望的值。

如果年份设置为Nothing的实例无法达到此方法,则只需使用Value属性即可。

我仍然不完全理解为什么会收到这个奇怪的错误消息。也许你可以看看所谓的实际方法是什么?指向该方法应该为您提供该信息。它不能是Nullable(Of Integer).ToString

答案 1 :(得分:0)

嗯,由于@ Nitram的评论,我找到了一个可行的解决方案和一些答案。年/月属性的类型已从Integer更改为Integer?。显然,这不是一个非常令人满意的答案,因为我仍然不明白为什么nullable int不能被格式化,但代码编译完美。我的工作解决方案一直在String上使用静态格式方法:

expirationDate = String.Format("{0:D4}-{1:D2}", model.Year, model.Month)

即使是可以为空的类型也可以正常工作。