GCM registration error for Android

I have a device that is already registered with GCM. I tried searching, but I couldn't find any similar problems. Most users have trouble getting getRegistrationId to return a non-empty string. Any ideas on why this is causing the error?

public class UploadOption extends Activity{
    Intent extras = getIntent();
    String optionid = extras.getStringExtra("KEY_OPTION");
    String duration = extras.getStringExtra("KEY_DURATION");
    String reward = extras.getStringExtra("KEY_REWARD");


    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.debug_layout);

        final String regId = GCMRegistrar.getRegistrationId(this);
        TextView tvDebug = (TextView) findViewById(R.id.tvDebug);
        tvDebug.setText(regId);

        // Stores option id, php grabs option.
        //new Upload().execute();
    }
}

      

The error log is below:

01-18 18:02:51.756: D/AndroidRuntime(4478): Shutting down VM
01-18 18:02:51.756: W/dalvikvm(4478): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
01-18 18:02:51.803: E/AndroidRuntime(4478): FATAL EXCEPTION: main
01-18 18:02:51.803: E/AndroidRuntime(4478): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.matt.drapp/com.matt.drapp.UploadOption}: java.lang.NullPointerException
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.os.Looper.loop(Looper.java:130)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at java.lang.reflect.Method.invoke(Method.java:507)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at dalvik.system.NativeStart.main(Native Method)
01-18 18:02:51.803: E/AndroidRuntime(4478): Caused by: java.lang.NullPointerException
01-18 18:02:51.803: E/AndroidRuntime(4478):     at com.matt.drapp.UploadOption.<init>(UploadOption.java:32)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at java.lang.Class.newInstanceImpl(Native Method)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at java.lang.Class.newInstance(Class.java:1409)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-18 18:02:51.803: E/AndroidRuntime(4478):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
01-18 18:02:51.803: E/AndroidRuntime(4478):     ... 11 more

      

+3


source to share


3 answers


The problem is with the code Intent

. Remember that the activity is only initialized after onCreate()

and you are trying to get the Intent values ​​before the activity is initialized.

Place below code inside onCreate();



Intent extras = getIntent();
String optionid = extras.getStringExtra("KEY_OPTION");
String duration = extras.getStringExtra("KEY_DURATION");
String reward = extras.getStringExtra("KEY_REWARD");

      

It should work.

+3


source


Which line in your example throws a NullPointerException. Because if that just textView is not found, it could crash the whole application just because you are trying to set the text and it is a null object.



So first comment out the textview stuff and see if everything works. If this is not the case, could you please tell us which line is line 32 in your class, as this is obviously not the same as here.

+1


source


The problem is your getRegistratonID is returning null. This is because you are calling it on your activity class before you register with the GCM server and receive the onRegistered callback on your GCMIntentService. If getRegistrationID returns null, you must run AsyncTask to call GCMRegistrar.register.

Follow the instructions here :

0


source







All Articles