安卓:参考到活动对象在另一个类成为空 [英] Android: Reference to Activity object in another class becomes NULL

查看:186
本文介绍了安卓:参考到活动对象在另一个类成为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是要在MainActivity的引用传递给另一个类,并使用它。

当我第一次通过引用不是null。但后来当我在事件处​​理程序使用它,它变成空。

这里是code:

 公共类MainActivity扩展MapActivity实现SmsReceivedListener {
    SmsReceiver smsreceiver =新SmsReceiver();    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        smsreceiver.setSmsReceivedListener(本);
    }    公共无效drawme(){
        //做未便
    }    @覆盖
    公共无效onSmsReceived(ID字符串,字符串LON,字符串LAT){
        Toast.makeText(这一点,短信发送,Toast.LENGTH_LONG).show();    }
}

其他类在那里我获得参考:

 公共类SmsReceiver扩展广播接收器{
    私人SmsReceivedListener侦听器;
    私有静态最后弦乐SMS_RECEIVED =android.provider.Telephony.SMS_RECEIVED;    @燮pressLint(NewApi)
    @覆盖
    公共无效的onReceive(最终上下文的背景下,最终的意图意图){
        //这是NULL
        如果(听众!= NULL)
            听众()onSmsReceived(carNumber,LON,LAT)。
    }    公共无效setSmsReceivedListener(SmsReceivedListener mainActivity){
        //这是没有空当我第一次设置对象这里
        监听=(mainActivity);
    }
}


解决方案

您没有注册SmsReceiver,为此我猜以下内容:

该SmsReceiver注册的清单。因此,接收机的一个实例是由系统创建检测到广播时,它是不一样的实例作为创建所述一个

建议:注册接收机中的活性,在清单中不是

What i am trying to do is to pass a reference to the mainactivity to another class and use it.

when i first pass the reference it is not null. but later when i use it in an event handler it becomes null.

here is the code:

public class MainActivity extends MapActivity implements SmsReceivedListener {
    SmsReceiver smsreceiver = new SmsReceiver();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        smsreceiver.setSmsReceivedListener(this);
    }

    public void drawme() {
        // do smth
    }

    @Override
    public void onSmsReceived(String id, String lon, String lat) {
        Toast.makeText(this, "SMS delivered", Toast.LENGTH_LONG).show();

    }
}

The other class where I obtain the reference:

public class SmsReceiver extends BroadcastReceiver {
    private SmsReceivedListener listener;
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @SuppressLint("NewApi") 
    @Override
    public void onReceive(final Context context, final Intent intent) {
        // Here it is NULL
        if(listener!=null)
            listener().onSmsReceived(carNumber, lon,lat);
    }

    public void setSmsReceivedListener(SmsReceivedListener mainActivity) {
        //It is no null when I first set the object here
        listener = (mainActivity);
    }
}

解决方案

You are not registering the SmsReceiver, therefor i guess the following :

The SmsReceiver is registered in the Manifest. Therefore, an instance of the receiver is created by the system when the broadcast is detected, and it is not the same instance as the one you create.

Suggestion : register the receiver in the activity, not in the manifest.

这篇关于安卓:参考到活动对象在另一个类成为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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