昨天遇到的一個問題,就是我在主線程中生成了handler對象,但是在下邊進行消息的發送的時候意外的報了異常,說是
e/androidruntime( 1819): java.lang.runtimeexception: can't create handler inside thread that has not called looper.prepare()
e/androidruntime( 1819): at android.os.handler.(handler.java:121)
e/androidruntime( 1819): at android.widget.toast.(toast.java:68)
e/androidruntime( 1819): at android.widget.toast.maketext(toast.java:231)
意思就是說不能建立一個handler來代替線程中沒有被調用looper.prepare(),然後我在代碼中添加了
looper.prepare(),looper.loop()這倆個方法。
package cn.wisenergy.tp;
import java.util.arraylist;
import java.util.list;
import org.apache.http.namevaluepair;
import org.apache.http.message.basicnamevaluepair;
import org.json.jsonexception;
import org.json.jsonobject;
import android.app.activity;
import android.content.context;
import android.content.intent;
import android.content.sharedpreferences;
import android.content.sharedpreferences.editor;
import android.os.bundle;
import android.os.handler;
import android.os.looper;
import android.util.log;
import android.view.menu;
import android.view.view;
import android.view.window;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;
import android.widget.toast;
import cn.wisenergy.tp.commonality.httpclienthelper;
import cn.wisenergy.tp.fragment.mainfragment2;
import cn.wisenergy.tp.fragment_person.choosefriendactivity;
import cn.wisenergy.tp.url.urlhelp;
public class loginactivity extends activity implements onclicklistener {
private textview login_binding;
private textview login_binding_zhuce;
private textview login_username;
private edittext login_password;
private button btn_login;
private sharedpreferences spf;
private string name ;
private int userid;
private handler handler = new handler() {
public void handlemessage(android.os.message msg) {
switch (msg.what) {
case 0:
toast.maketext(loginactivity.this, "登陸成功", toast.length_short).show();
editor edit=spf.edit();
edit.putint("userid", userid);
edit.putstring("username", name);
edit.putstring("password", login_password.gettext().tostring());
edit.commit();
intent intent = new intent();
intent.setclass(loginactivity.this, mainfragment2.class);
startactivity(intent);
finish();
break;
case 1:
toast.maketext(loginactivity.this, "登陸失敗", toast.length_short)
.show();
}
};
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
this.requestwindowfeature(window.feature_no_title);
spf= getsharedpreferences("accountinfo", context.mode_private);
name=spf.getstring("username", null);
setcontentview(r.layout.login);
initviews();
public void initviews() {
login_username =(textview)findviewbyid(r.id.login_username);
login_username.settext(name);
login_password =(edittext)findviewbyid(r.id.login_password);
login_binding = (textview) findviewbyid(r.id.login_binding);
btn_login = (button) findviewbyid(r.id.btn_logins);
login_binding_zhuce = (textview) findviewbyid(r.id.login_binding_zhuce);
login_binding.setonclicklistener(this);
btn_login.setonclicklistener(this);
login_binding_zhuce.setonclicklistener(this);
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.login, menu);
return true;
public void onclick(view v) {
switch (v.getid()) {
case r.id.login_binding:
// 切換到其它登陸方式
intent intent = new intent(loginactivity.this, login_bindingactivity.class);
case r.id.btn_logins:
// 登陸
submitlogin_info(name,login_password.gettext().tostring());
case r.id.login_binding_zhuce:
//去注冊
intent intent2 = new intent(loginactivity.this, regactivity.class);
startactivity(intent2);
default:
public int relateresult(string str) throws jsonexception {
if (str != null && !str.equals("")) {
jsonobject json = new jsonobject(str);
if (json != null) {
if (!json.isnull("status")) {
jsonobject status = json.getjsonobject("status");//
if (!status.isnull("code")) {
int code = status.getint("code");
return code;
} else {
log.e("json", "沒有code這個value");
log.e("json", "json資料為空");
return 0;
public int relateresult1(string str) throws jsonexception {
int userid=json.getint("data");
return userid;
}
public void submitlogin_info(final string name,
final string password) {
new thread(new runnable() {
public void run() {
looper.prepare();
string result = httpclienthelper.loadtextfromurl(urlhelp.login+"loginname="+name+"&password="+password,name+":"+password,
loginactivity.this);
log.d("result", result);
if (result != null) {
int code;
try {
code = relateresult(result);
log.d("code", code+"");
if (code == 2000) {
userid = relateresult1(result);
handler.sendemptymessage(0);
} else if (code == 4000) {
toast.maketext(loginactivity.this,
"指定使用者不存在", toast.length_short).show();
} else if (code == 4001) {
"使用者密碼錯誤", toast.length_short).show();
} else if (code == 4002) {
"目前使用者已被鎖定不可登入(超出最大登入次數)",
toast.length_short).show();
} else if (code == 4003) {
"使用者沒有權限通路該資源", toast.length_short).show();
} else if (code == 4004) {
"使用者認證資訊已過期,需要更新", toast.length_short)
} else if (code == 4005) {
"使用者未重新設定初試密碼", toast.length_short).show();
} else if (code == 4006) {
"目前使用者已被禁用(違規操作,管理者手動禁用)",
} else if (code == 4007) {
"接口參數非法:參數長度超出允許的最大值", toast.length_short)
} else if (code == 5009) {
"接口參數非法:參數長度超出允許的最小值", toast.length_short)
} catch (jsonexception e) {
e.printstacktrace();
looper.loop();
}).start();