天天看点

主线程中的handle问题

昨天遇到的一个问题,就是我在主线程中生成了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();