如何在Android中根据时间填充弹出窗口,然后在20秒后隐藏?
这是我的代码:
public class ExPopup extends Activity {
Dialog myDialog;
Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(ExPopup.this);
helpBuilder.setTitle("Pop Up");
helpBuilder.setMessage("This is a Simple Pop Up");
helpBuilder.setPositiveButton("Positive",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
}
});
helpBuilder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
helpBuilder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
final AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
Timer tShow = new Timer();
final Timer tHide = new Timer();
// This timer will show the popup every 10 second
tShow.schedule(new TimerTask(){
public void run(){
helpDialog.show();
Date d = new Date(0);
d.setSeconds(d.getSeconds()+20);
//This will hide the popup after 20 secnds
tHide.schedule(new TimerTask(){
public void run(){
helpDialog.dismiss();
}
},d);
}
},0,10*1000);
}
}
嘿,我遇到了这个错误,应用程序被强制
关闭:
03-27 10:55:21.371: E/AndroidRuntime(523): FATAL EXCEPTION: Timer-0
03-27 10:55:21.371: E/AndroidRuntime(523): java.lang.IllegalArgumentException
03-27 10:55:21.371: E/AndroidRuntime(523): at java.sql.Date.getSeconds(Date.java:109)
03-27 10:55:21.371: E/AndroidRuntime(523): at com.example.android.ExPopup$4.run(ExPopup.java:60)
03-27 10:55:21.371: E/AndroidRuntime(523): at java.util.Timer$TimerImpl.run(Timer.java:284)
03-27 10:55:24.601: E/WindowManager(523): Activity com.example.android.ExPopup has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051bd00 that was originally added here
03-27 10:55:24.601: E/WindowManager(523): android.view.WindowLeaked: Activity com.example.android.ExPopup has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4051bd00 that was originally added here
03-27 10:55:24.601: E/WindowManager(523): at android.view.ViewRoot.<init>(ViewRoot.java:258)
03-27 10:55:24.601: E/WindowManager(523): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-27 10:55:24.601: E/WindowManager(523): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-27 10:55:24.601: E/WindowManager(523): at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-27 10:55:24.601: E/WindowManager(523): at android.app.Dialog.show(Dialog.java:241)
03-27 10:55:24.601: E/WindowManager(523): at com.example.android.ExPopup.onCreate(ExPopup.java:50)
03-27 10:55:24.601: E/WindowManager(523): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-27 10:55:24.601: E/WindowManager(523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-27 10:55:24.601: E/WindowManager(523): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-27 10:55:24.601: E/WindowManager(523): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-27 10:55:24.601: E/WindowManager(523): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-27 10:55:24.601: E/WindowManager(523): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 10:55:24.601: E/WindowManager(523): at android.os.Looper.loop(Looper.java:123)
03-27 10:55:24.601: E/WindowManager(523): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-27 10:55:24.601: E/WindowManager(523): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 10:55:24.601: E/WindowManager(523): at java.lang.reflect.Method.invoke(Method.java:507)
03-27 10:55:24.601: E/WindowManager(523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-27 10:55:24.601: E/WindowManager(523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-27 10:55:24.601: E/WindowManager(523): at dalvik.system.NativeStart.main(Native Method)
03-27 10:55:25.941: I/Process(523): Sending signal. PID: 523 SIG: 9