我正在使用文本视图,我想在我在设定的日期运行应用程序时显示倒数计时器。
<?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代码是名为countdown的文本视图。
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);
是否我无法在一个文本视图中显示几小时的分钟和秒。我的问题是,当我运行应用程序时,计时器不会显示在应用程序上。非常感谢任何帮助。
答案 0 :(得分:2)
dateFormat
变量为空
您尚未在SimpleDateFormat
阻止
try{ } catch () {}
的实例
要更正此问题的示例代码:
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");
}