如何在片段内的textview中设置当前日期和时间?

时间:2017-01-18 02:13:34

标签: java android simpledateformat

有一个重复的问题,但我们对如何实施它有不同的看法。一旦应用程序运行,我将自动显示,除了表格之外,日期的textview上面是我在片段中实现的Material Calendar Design。所以我的问题就是自己的标题。

继承我在 ScheduleFragment.java

中的代码
package com.example.guitarista.citem;


import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.icu.util.TimeZone;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.util.Date;


/**
 * A simple {@link Fragment} subclass.
 */
public class ScheduleFragment extends Fragment {


    public ScheduleFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getActivity().setTitle("My Schedule");
        View v = inflater.inflate(R.layout.fragment_schedule, container, false);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String currentDateandTime = sdf.format(new Date());
        TextView tv = (TextView) v.findViewById(R.id.textDate2);
        tv.setText(currentDateandTime);

        final MaterialCalendarView materialCalendarView = (MaterialCalendarView) v.findViewById(R.id.calendarView);
        materialCalendarView.state().edit()
                .setFirstDayOfWeek(Calendar.MONDAY)
                .setMinimumDate(CalendarDay.from(1900, 1, 1))
                .setMaximumDate(CalendarDay.from(2100, 12, 31))
                .setCalendarDisplayMode(CalendarMode.MONTHS)
                .commit();

        materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                //Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show();
                if (materialCalendarView.getSelectedDate().equals(CalendarDay.from(2017, 0, 1))) {
                    Toast.makeText(getActivity(), "NEW YEAR!", Toast.LENGTH_SHORT).show();
                }
                else if (materialCalendarView.getSelectedDate().equals(CalendarDay.from(2017, 1, 14))) {
                    Toast.makeText(getActivity(), "Happy Valentines Day!", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return v;
    }

}

我知道已经存在用于设置当前日期和时间的代码,但应用程序无法正常运行(例如“App停止工作”),那么你们有其他解决方案吗?

这是 logcat

01-18 10:39:05.061 26486-26486/? I/art: Late-enabling -Xcheck:jni
01-18 10:39:05.071 26486-26486/? V/appproc: App process: starting thread pool.
01-18 10:39:05.089 26486-26500/? E/art: Failed sending reply to debugger: Broken pipe
01-18 10:39:05.090 26486-26500/? I/art: Debugger is no longer active
01-18 10:39:05.094 26486-26486/? D/Proxy: setHttpRequestCheckHandler
01-18 10:39:05.109 26486-26486/? D/wangcy9: setStatusIcon occur wrong theme!
01-18 10:39:05.184 26486-26509/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-18 10:39:05.188 26486-26486/? D/ViewRootImpl: loadSystemProperties PersistDebugEvent: false RoDebugEvent: false
01-18 10:39:05.218 26486-26509/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017_msm8916_64_LA.BR.1.1.3_RB1__release_AU ()
                                               OpenGL ES Shader Compiler Version: E031.25.03.04
                                               Build Date: 04/29/15 Wed
                                               Local Branch: mybranch9502464
                                               Remote Branch: quic/LA.BR.1.1.3_rb1.12
                                               Local Patches: NONE
                                               Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017 + 26a3cba + 6f69ea6 + 8bc2bc8 + 649fcde + a52cccf + dbf281f + 15f0bf8 +  NOTHING
01-18 10:39:05.219 26486-26509/? I/OpenGLRenderer: Initialized EGL, version 1.4
01-18 10:39:05.229 26486-26509/? D/OpenGLRenderer: Enabling debug mode 0
01-18 10:39:05.234 26486-26509/? I/qdutils: PartialUpdate status: Disabled
01-18 10:39:05.234 26486-26509/? I/qdutils: Left Align: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Width Align: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Top Align: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Height Align: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Min ROI Width: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Min ROI Height: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Needs ROI Merge: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Dynamic Fps: Disabled
01-18 10:39:05.234 26486-26509/? I/qdutils: Min Panel fps: 0
01-18 10:39:05.234 26486-26509/? I/qdutils: Max Panel fps: 0
01-18 10:39:05.321 26486-26486/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@12412c01 time:193527594
01-18 10:39:09.889 26486-26486/com.example.guitarista.citem I/Timeline: Timeline: Activity_launch_request id:com.example.guitarista.citem time:193532162
01-18 10:39:09.896 26486-26486/com.example.guitarista.citem I/Choreographer: Skipped 36 frames!  The application may be doing too much work on its main thread.
01-18 10:39:09.926 26486-26486/com.example.guitarista.citem W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-18 10:39:09.940 26486-26486/com.example.guitarista.citem D/wangcy9: setStatusIcon occur wrong theme!
01-18 10:39:10.228 26486-26505/com.example.guitarista.citem I/art: Background partial concurrent mark sweep GC freed 1161(54KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 48MB/81MB, paused 5.124ms total 22.066ms
01-18 10:39:10.309 26486-26486/com.example.guitarista.citem D/ViewRootImpl: loadSystemProperties PersistDebugEvent: false RoDebugEvent: false
01-18 10:39:10.457 26486-26486/com.example.guitarista.citem I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@202b8645 time:193532731
01-18 10:39:14.121 26486-26486/com.example.guitarista.citem D/AndroidRuntime: Shutting down VM
01-18 10:39:14.122 26486-26486/com.example.guitarista.citem E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.guitarista.citem, PID: 26486
                                                                              java.lang.NoClassDefFoundError: Failed resolution of: Landroid/icu/text/SimpleDateFormat;
                                                                                  at com.example.guitarista.citem.ScheduleFragment.onCreateView(ScheduleFragment.java:46)
                                                                                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                                                                                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
                                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5322)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                               Caused by: java.lang.ClassNotFoundException: Didn't find class "android.icu.text.SimpleDateFormat" on path: DexPathList[[zip file "/data/app/com.example.guitarista.citem-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
                                                                                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                                  at com.example.guitarista.citem.ScheduleFragment.onCreateView(ScheduleFragment.java:46) 
                                                                                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290) 
                                                                                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
                                                                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677) 
                                                                                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536) 
                                                                                  at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5322) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
                                                                                Suppressed: java.lang.ClassNotFoundException: android.icu.text.SimpleDateFormat
                                                                                  at java.lang.Class.classForName(Native Method)
                                                                                  at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                                  at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                        ... 16 more
                                                                               Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
01-18 10:39:17.882 26486-26486/com.example.guitarista.citem I/Process: Sending signal. PID: 26486 SIG: 9

2 个答案:

答案 0 :(得分:3)

你导入了错误的库。 走到你的片段顶部,改变

import android.icu.text.SimpleDateFormat;

import java.text.SimpleDateFormat;

答案 1 :(得分:1)

此代码不会产生任何崩溃并且对我来说非常完美。

long date = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("MMM MM dd, yyyy h:mm a"); String dateString = sdf.format(date); textView.setText(dateString);