Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法设置回调侦听器。在构造函数中,值是可以的,但内部方法始终为null_Android_Interface_Constructor_Callback_Listener - Fatal编程技术网

Android 无法设置回调侦听器。在构造函数中,值是可以的,但内部方法始终为null

Android 无法设置回调侦听器。在构造函数中,值是可以的,但内部方法始终为null,android,interface,constructor,callback,listener,Android,Interface,Constructor,Callback,Listener,我不知道我哪里错了。我已经为我的一个类class a设置了一个监听器,在这里我保存了一些用户信息 回到主活动类B,我实现了第一个类,并初始化了侦听器。然后在类B中,我为接口创建了一个构造函数来完成初始化,这里监听器的值是ok的:初始化时监听器的值:com.fideli。MainActivity@425b0500,但在我想使用的方法中,我总是得到null,我的应用程序崩溃 我错在哪里?谢谢 A类: public class GCMActivity { private final stat

我不知道我哪里错了。我已经为我的一个类class a设置了一个监听器,在这里我保存了一些用户信息

回到主活动类B,我实现了第一个类,并初始化了侦听器。然后在类B中,我为接口创建了一个构造函数来完成初始化,这里监听器的值是ok的:初始化时监听器的值:com.fideli。MainActivity@425b0500,但在我想使用的方法中,我总是得到null,我的应用程序崩溃

我错在哪里?谢谢

A类:

public class GCMActivity {

    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    Context context; 


    public void setCallback(regidListener rListener) {
        this.rListener = rListener;     
        //here it is ok, it is not null
        System.out.println("value of the listeneris on initialisation: " + rListener);
    }

    public static interface regidListener {
        public void onRegIdSaved(String regId);
    }

    public regidListener rListener; 

    public GCMActivity(Context context) {
        this.context = context;
    }

    public void registerIfNeeded() {
        // here is already null
        System.out.println("value of the listeneris: " + this.rListener);
        if (rListener != null){
        rListener.onRegIdSaved("HEY!!");    
        }

        if (checkPlayServices()) {           
            gcm = GoogleCloudMessaging.getInstance(context);
            regid = getRegistrationId(context);
            System.out.println("Class Started!!");
            if (regid == null) {
                registerInBackground();
            }
B类主要活动:

public class MainActivity extends FragmentActivity implements GCMActivity.regidListener  {


...

@Override
    protected void onResume() {
        super.onResume();
        uiHelper.onResume();            
        if(!isOnline()){
            showGpsButton();            
         }      

        //initialising listener for regId ready
        GCMActivity gcm = new GCMActivity(this);
        gcm.setCallback(this);

    }


@Override
    public void onRegIdSaved(String regId) {
        System.out.println("regId ready" + regId);

    }

....

是不是因为在onResume方法中,gcm是一个局部变量,当onResume返回时,它超出了范围?似乎您需要将其设置为类变量,例如mGcm。

也许,经过一些实验后,我删除了setCallback方法,并将初始化移到了默认构造函数中,它成功了!但是我想使用setCallback方法,而且大多数都是为了理解这种行为。