处理程序中的Toast问题

时间:2014-01-28 23:26:00

标签: android

我很想得到一个处理程序显示Toast messgae,它还包含从msg.obj中提取的变量。它不会给我任何错误,但Toast消息没有显示。这是代码

 Handler nok=new Handler() {

     public void handleMessage(Message msg)


    {



Location locator=(Location)msg.obj;
float lat=(float)(locator.getLatitude());
Toast.makeText(m,"the latitude is+lat,Toast.LENGTH_LONG).show();


}

我也尝试添加

    runOnUiThread(new Runnable()

{

public void run (){


Toast.makeText(m,"the latitude is" +lat,Toast.LENGTH_LONG).show();

}

但是在这种情况下,变量lat在Handler之外无法识别,有什么想法吗?

整个班级

public class MainActivity extends Activity {

public static final String content="Asega kvo stava";
public static final Integer kom=2;
 Context m;
 Looper lupy;
 Handler ok;
Handler nok=new Handler() {

     public void handleMessage(Message msg)


    {
         System.out.println("Check"); 


         Location locator=(Location)msg.obj;
            float lat=(float)(locator.getLatitude());
 Toast.makeText(MainActivity.this,"the latitude is"+" "+lat,Toast.LENGTH_LONG).show();
        }

    };

和发布消息的类:

public class  locac implements LocationListener  {

public Context Ctx;
Handler nok;
public static String okai="Ctx.LOCATION_SERVICE";
public static String moi="manag.GPS_PROVIDER";


   Location loc;

public locac (Context context,Handler handler)


{


   this.nok=handler;

    this.Ctx=context;

}



public void getLoc (Location Loc )

{
    if(loc!=null)

    {
        LocationManager manag;

         manag=(LocationManager)Ctx.getSystemService(okai);

         Message kol=new Message();
         kol.obj=loc;
         nok.sendMessage(kol);
    }


}

2 个答案:

答案 0 :(得分:0)

Handler nok=new Handler() {

     public void handleMessage(Message msg)


    {



Location locator=(Location)msg.obj;
float lat=(float)(locator.getLatitude());
Toast.makeText(getApplicationContext(), "the latitude is" + " " +lat, Toast.LENGTH_LONG).show();


}

尝试使用它。

答案 1 :(得分:0)

你确定m被设置为你的Activity的上下文吗?您确定要正确发送消息并且调用handleMessage()吗?