我想創建一個線程來處理登錄按鈕被按下時執行的登錄函數,以便我可以顯示progressDialog。在Android應用程序創建線程結果強制關閉
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Check Login
ProgressDialog.show(Activity.this, "", "Loading...");
new Thread(new Runnable(){
public void run(){
try{
int duration;
Toast toast;
Context context;
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
JSONArray jsonArray=null;
password=ServerConnection.encryptPassword(password);
//Log.i("login hash",password);
jsonArray=ServerConnection.login(username, password);
if(jsonArray!=null) //login successful
{
context=getApplicationContext();
duration=Toast.LENGTH_SHORT;
toast=Toast.makeText(context, "Login Successful!", duration);
toast.show();//Shows the little pop up right at the bottom of the screen
Intent i = new Intent(getApplicationContext(), MapWithFriends.class);
startActivity(i);
}
else
{
context=getApplicationContext();
duration=Toast.LENGTH_SHORT;
toast=Toast.makeText(context, "Login Fail", duration);
toast.show();//Shows the little pop up right at the bottom of the screen
//lblResult.setText("Login failed. Username and/or password doesn't match.");
}
}catch(Exception e)
{
Log.e("tag", e.getMessage());
}
progressDialog.dismiss();
}
}).start();
}
});
但是,當線程被創建時,它強制關閉。如果我改回沒有線程,它工作正常。
感謝
編輯:
logcat的崩潰:螺紋10
顯示java.lang.NullPointerException:
不能內螺紋已不叫Looper.prepare()
致命異常處理程序創建
..... 你們會需要比這更多嗎?
給我們崩潰的堆棧跟蹤,我們不是魔術師。 :) –
哦,對不起,現在加入! – Dan