经过的部位与不同类处理程序 [英] passing location with handler from different class

查看:160
本文介绍了经过的部位与不同类处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来处理程序的概念,我读了不少关于android.Most实例句柄虽然是2个线程在活动中既类开始,我想从所谓的Locac但另一个类传递位置给我空指针异常。

这里是Activity类

 活套卢皮;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
dbadapter mkola =新dbadapter(MainActivity.this);
mkola.openToWrite();
 处理器NOK =新的处理程序(卢皮){     公共无效的handleMessage(消息MSG)
    {        位置定位器=(位置)msg.obj;
        浮动纬度=(浮点)(locator.getLatitude());    }};

此处是位置类(通知我故意没有包括为了简单起见任何线程)

 公共类locac实现LocationListener的{公共语境了Ctx;
处理器NOK;
公共静态字符串冈井=Ctx.LOCATION_SERVICE;
公共静态字符串MOI =manag.GPS_PROVIDER;公共locac(上下文的背景下)
{
this.Ctx =背景;}@覆盖
公共无效onLocationChanged(位置LOC){
    // TODO自动生成方法存根
     的LocationManager MANAG;     MANAG =(的LocationManager)Ctx.getSystemService(冈井);     消息KOL =新的Message();
     kol.obj =禄;
     nok.sendMessage(KOL);
}

和我在LogCat中得到

 致:显示java.lang.NullPointerException
在android.os.Handler<&初始化GT;(Handler.java:226)
在android.os.Handler<&初始化GT;(Handler.java:134)
在com.example.alarma.MainActivity $ 1<&初始化GT;(MainActivity.java:39)
在com.example.alarma.MainActivity.onCreate(MainActivity.java:39)


解决方案

有其他问题在这里,但主要的问题是,你的处理程序未初始化。您需要在构建您对象从您的活动传递

 公共类locac实现LocationListener的{公共静态字符串冈井=Ctx.LOCATION_SERVICE;
公共静态字符串MOI =manag.GPS_PROVIDER;公共语境了Ctx;
处理器NOK;公共locac(上下文的背景下,处理程序处理){
     this.Ctx =背景;
     this.nok =处理程序;
}@覆盖
公共无效onLocationChanged(位置LOC){
    // TODO自动生成方法存根
     的LocationManager MANAG;     MANAG =(的LocationManager)Ctx.getSystemService(冈井);     消息KOL =新的Message();
     kol.obj =禄;
     nok.sendMessage(KOL);
}

I am new to the concept of Handlers, and i read quite a bit about handlers in android.Most examples though are with 2 threads both starting in the activity class, i want to pass the location from another class called Locac but it gives me NullPointer exception.

here is the Activity class

    Looper lupy;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


dbadapter mkola=new dbadapter(MainActivity.this);
mkola.openToWrite();






 Handler nok=new Handler(lupy) {

     public void handleMessage(Message msg)


    {

        Location locator=(Location)msg.obj;
        float lat=(float)(locator.getLatitude());

    }

};

here is the location class (notice i purposefully did not include any thread for the sake of simplicity)

public class  locac implements LocationListener  {

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


public static String moi="manag.GPS_PROVIDER";

public locac (Context context)


{
this.Ctx=context;

}

@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub


     LocationManager manag;

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

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

and i get in the LogCat

 caused by: java.lang.NullPointerException
at android.os.Handler.<init>(Handler.java:226)
at android.os.Handler.<init>(Handler.java:134)
at com.example.alarma.MainActivity$1.<init>(MainActivity.java:39)
at com.example.alarma.MainActivity.onCreate(MainActivity.java:39)

解决方案

There are other issues here, but the main issue is that your handler isn't initialized. You need to pass it from your activity when you construct your Object:

public class  locac implements LocationListener  {

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

public Context Ctx;
Handler nok;

public locac (Context context, Handler handler){
     this.Ctx=context;
     this.nok = handler;
}

@Override
public void onLocationChanged(Location loc) {
    // TODO Auto-generated method stub


     LocationManager manag;

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

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

这篇关于经过的部位与不同类处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆