我正在尝试在线程中创建警报框,但这里出现错误是我的线程和警报框代码,这个警报框成功地解决了线程。发布代码和错误日志,帮助我。
Thread paypalThread = new Thread() {
@Override
public void run() {
try {
PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo(
"com.mothistorycheck", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified();
Date date = new Date(installed * 1000L);
Date currentDate = Calendar.getInstance().getTime();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, 1);
Date installedPlusYear = cal.getTime();
System.out.println(installedPlusYear);
if (currentDate.compareTo(date)==-1) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
}).create();
// Setting Dialog Title
alertDialog.setTitle("Alert");
// Setting Dialog Message
alertDialog
.setMessage("Either MOT test number or Document Reference Number should be entered!");
alertDialog.setCancelable(true);
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Showing Alert Message
alertDialog.show();
return;
}else
System.out.println("000000");
// int comparison2 = oneYearFromNow.compareTo(date);
// sleep(31536000);
}
catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// finish();
Intent i = new Intent(getApplicationContext(),
SplashActivity.class);
//startActivity(i);
}
}
};
paypalThread.start();
这里记录详细信息
10-24 12:32:23.462: E/AndroidRuntime(20705): FATAL EXCEPTION: Thread-5842
10-24 12:32:23.462: E/AndroidRuntime(20705): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-24 12:32:23.462: E/AndroidRuntime(20705): at android.os.Handler.<init>(Handler.java:121)
10-24 12:32:23.462: E/AndroidRuntime(20705): at android.app.Dialog.<init>(Dialog.java:107)
10-24 12:32:23.462: E/AndroidRuntime(20705): at android.app.AlertDialog.<init>(AlertDialog.java:114)
10-24 12:32:23.462: E/AndroidRuntime(20705): at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
10-24 12:32:23.462: E/AndroidRuntime(20705): at com.mothistorycheck.MainActivity$1.run(MainActivity.java:132)