从非活动类获取GCM REG ID

时间:2014-04-02 13:49:07

标签: android push-notification google-cloud-messaging

我建了一个班级"简介"这不是扩展活动类。 如何在该课程中获得GCM注册ID。我已经在mainActivity类中获得了GCM注册ID。 我的代码在

之下
import com.google.android.gcm.GCMRegistrar;
public class Profile extends Fragment implements Common{
Context context;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context); 
    final String regId = GCMRegistrar.getRegistrationId(context);
    Log.v("GCM ID", "GCM ID is " + regId);
    }
}

我在onCreatview中编写了一些代码,但是在这段代码(GCMRegistrar)之后我的应用程序崩溃了。

请帮帮我。

错误是

04-02 18:45:40.969: E/AndroidRuntime(8778): FATAL EXCEPTION: main
04-02 18:45:40.969: E/AndroidRuntime(8778): java.lang.NullPointerException
04-02 18:45:40.969: E/AndroidRuntime(8778):     at        com.google.android.gcm.GCMRegistrar.getGCMPreferences(GCMRegistrar.java:502)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at    com.google.android.gcm.GCMRegistrar.getRegistrationId(GCMRegistrar.java:330)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at com.afixi.aap_leader_profile.Profile.onCreateView(Profile.java:225)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.os.Handler.handleCallback(Handler.java:615)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at android.os.Looper.loop(Looper.java:137)
04-02 18:45:40.969: E/AndroidRuntime(8778):at android.app.ActivityThread.main(ActivityThread.java:4960)
04-02 18:45:40.969: E/AndroidRuntime(8778):at java.lang.reflect.Method.invokeNative(Native Method)
04-02 18:45:40.969: E/AndroidRuntime(8778):at java.lang.reflect.Method.invoke(Method.java:511)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
04-02 18:45:40.969: E/AndroidRuntime(8778):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

GCMRegistrar已弃用。使用GCM API。

但是如果你想继续使用它,请显示你的LogCat。我认为它是依赖项或线程的东西,因为GCMRegistrar是异步的。

答案 1 :(得分:0)

您的context变量为空。您永远不会初始化它,但将其传递给其他方法。这是NullPointerException的原因。您可以将片段的活动用作上下文:

import com.google.android.gcm.GCMRegistrar;
public class Profile extends Fragment implements Common{
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    GCMRegistrar.checkDevice(getActivity());
    GCMRegistrar.checkManifest(getActivity()); 
    final String regId = GCMRegistrar.getRegistrationId(getActivity());
    Log.v("GCM ID", "GCM ID is " + regId);
    }
}