handler传递数据咋收不到

郎里个郎里个郎 发布于 2013/04/18 20:45
阅读 486
收藏 0
package com.example.date04_17;
//此程序对应的是手机归属的查询
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
TextView show;
EditText edittext;
private Handler messageHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

show = (TextView)this.findViewById(R.id.text);
edittext = (EditText)this.findViewById(R.id.ed01); 
Looper looper = Looper.myLooper();
messageHandler = new MessageHandler(looper);
}
//处理按钮事件
public void save(View v) {
new Thread(){
public void run(){
String mobile = edittext.getText().toString();
//调用AdressService的方法获得地址
String mymobile;
try {
mymobile = AdressService.getAdress(mobile);
Message message = Message.obtain();


             message.obj = mymobile;
             messageHandler.sendMessage(message);
} catch (Exception e){
e.printStackTrace();


}
}.start();
}
@SuppressLint("HandlerLeak")
class MessageHandler extends Handler {
   public MessageHandler(Looper looper) {
       super(looper);
   }
   @SuppressLint("ShowToast")

public void handleMessage(Message msg) { 

//Toast显示没有值

    Toast.makeText(getApplicationContext(), (String) msg.obj, 1).show();

   }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}






加载中
0
okuraoy
okuraoy
应该new出个新的Message
返回顶部
顶部