嗨,我正在尝试执行下面的代码,假设打开一个加载对话框并在if
语句中将其关闭。
这是代码:
loginBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
final ProgressDialog progress = ProgressDialog.show(thisActivity, "Please wait", "Loading please wait..", true);
Thread loginThread = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
boolean userAllowed = login.loginUser(userEmail.getText().toString(), userPass.getText().toString());
if(userAllowed)
{
progress.dismiss();
startActivity(mainPage);
}
else
{
progress.dismiss();
Toast.makeText(context, "Invalide email and password", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(context, "There is some problem", Toast.LENGTH_LONG).show();
}
}
});
loginThread.start();
}
});
logCat 错误输出是:
05-09 22:37:26.508: E/AndroidRuntime(24820): FATAL EXCEPTION: Thread-1306
05-09 22:37:26.508: E/AndroidRuntime(24820): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.os.Handler.<init>(Handler.java:197)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.os.Handler.<init>(Handler.java:111)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.widget.Toast$TN.<init>(Toast.java:324)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.widget.Toast.<init>(Toast.java:91)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.widget.Toast.makeText(Toast.java:238)
05-09 22:37:26.508: E/AndroidRuntime(24820): at com.shale.activities.MainActivity$1$1.run(MainActivity.java:93)
05-09 22:37:26.508: E/AndroidRuntime(24820): at java.lang.Thread.run(Thread.java:856)
我的参考是本教程。
谢谢!