我正在尝试在我的应用程序中创建一个具有特定日期的倒数计时器

I am trying to create a countdown timer with a specific date in my application

我正在使用文本视图,我希望在设置日期 运行 应用程序时显示倒数计时器。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.project.MainActivity"
    android:id="@+id/drawer_layout">

    <LinearLayout

        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:weightSum="1">

        <include
            layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/countdown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Next Event:"
            android:textStyle="bold"
            android:textSize="25dp" />
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/navigation_view"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/navigation_drawer_header"
        ></android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

以上是我的 XML 应用程序代码,其中包含名为倒计时的文本视图。

    private TextView txtCountdownEvent;
    private Handler handler;
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        txtCountdownEvent = (TextView) findViewById(R.id.countdown);

        actionBarDrawerToggle = new

                ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.draw_close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);

        countDownStart();
    }



    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();

    }
    public void countDownStart() {
        handler = new Handler();
        runnable = new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void run() {
                handler.postDelayed(this, 1000);
                try {
                    SimpleDateFormat dateFormat = null;
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                        dateFormat = new SimpleDateFormat(
                                "yyyy-MM-dd");
                    }
// Please here set your event date//YYYY-MM-DD
                    Date futureDate = dateFormat.parse("2017-09-23");
                    Date currentDate = new Date();
                    if (!currentDate.after(futureDate)) {
                        long diff = futureDate.getTime()
                                - currentDate.getTime();
                        long days = diff / (24 * 60 * 60 * 1000);
                        diff -= days * (24 * 60 * 60 * 1000);
                        long hours = diff / (60 * 60 * 1000);
                        diff -= hours * (60 * 60 * 1000);
                        long minutes = diff / (60 * 1000);
                        diff -= minutes * (60 * 1000);
                        long seconds = diff / 1000;
                        txtCountdownEvent.setText("" + String.format("%02d", days, hours, minutes, seconds));

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        handler.postDelayed(runnable, 1 * 1000);

是不是我无法在一个文本视图中显示天时分秒。我遇到的问题是,当我 运行 应用程序时,计时器没有显示在应用程序上。非常感谢任何帮助。

dateFormat变量为空

您尚未在 try{ } catch () {}

中为 低于 Android 版本 N 的设备创建 SimpleDateFormat 实例

更正此问题的示例代码:

SimpleDateFormat dateFormat = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    dateFormat = new SimpleDateFormat("yyyy-MM-dd");
} else {
    //Set the optional Date format here for Devices Running ANDROID VERSION BELOW N
    dateFormat = new SimpleDateFormat("yyyy-MM-dd");
}