gpt4 book ai didi

java - Android:SimpleDateFormat 给出 API 错误

转载 作者:行者123 更新时间:2023-12-01 06:07:53 25 4
gpt4 key购买 nike

我在 Android 上使用 SimpleDateFormat。我用 API 24 测试了它,它有效。当我尝试在 API 16 上使用它时,它崩溃了。我在互联网上搜索过,发现有人说这可能是因为错误的导入语句( import android.icu.text.SimpleDateFormat ),但我已经在使用 import java.text.SimpleDateFormat 了。 .

我的代码(在 onclickButton 上崩溃):

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {

SimpleDateFormat sdf;
String date;

public void onclickButton (View view) {

Calendar c = Calendar.getInstance();
sdf = new SimpleDateFormat("dd MM YYYY");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
date = sdf.format(c.getTime());
}
...
}

堆栈跟踪:

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb111d228)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4084) 
at android.view.View$PerformClick.run(View.java:16966) 
at android.os.Handler.handleCallback(Handler.java:615) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y'
at java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:268)
at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:323)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:369)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:253)
at com.jonathan_tilly.adventskalender.MainActivity.onclickButton(MainActivity.java:34)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4084) 
at android.view.View$PerformClick.run(View.java:16966) 
at android.os.Handler.handleCallback(Handler.java:615) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

感谢任何帮助!

最佳答案

可能这是因为您在类之外声明变量。尝试:

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {

SimpleDateFormat sdf;
String date;

public void onclickButton (View view) {

Calendar c = Calendar.getInstance();
sdf = new SimpleDateFormat("dd MM yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
date = sdf.format(c.getTime());
}
...

}

关于java - Android:SimpleDateFormat 给出 API 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40809450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com