我在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)
感谢任何帮助!
答案 0 :(得分:0)
可能是因为你在课堂上声明了变量。尝试:
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());
}
...
}
答案 1 :(得分:-1)
我认为你应该使用
new SimpleDateFormat("dd MM yyyy");
而不是
new SimpleDateFormat("dd MM YYYY");
它对我有用
而且java doc说YYYY是一周的 Here