Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

java - Problems implementing the new GCM Client for Android

I'm trying to implement the new GCM Client on Android following the guidelines from Google (https://developers.google.com/cloud-messaging/android/client). I'm receiving slightly different errors for two different devices. I noticed that I receive NoSuchMethodError for method getNoBackupFilesDir for both the devices.
This is the logcat for an Android 4.2.2:

06-08 09:01:09.920: I/dalvikvm(9138): Could not find methodandroid.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve virtual method 273: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x00c2
06-08 09:01:09.920: I/dalvikvm(9138): DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve instance field 12
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x54 at 0x00e1
06-08 09:01:09.920: E/dalvikvm(9138): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve check-cast 25 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x1f at 0x000e
06-08 09:01:09.920: I/dalvikvm(9138): Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
06-08 09:01:09.920: W/dalvikvm(9138): VFY: unable to resolve virtual method 528: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
06-08 09:01:09.920: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x000b
06-08 09:01:09.950: I/dalvikvm(9138): Could not find method android.support.v4.content.ContextCompat.getNoBackupFilesDir, referenced from method com.google.android.gms.iid.zzd.zzde
06-08 09:01:09.950: W/dalvikvm(9138): VFY: unable to resolve virtual method 2080: Landroid/support/v4/content/ContextCompat;.getNoBackupFilesDir (Landroid/content/Context;)Ljava/io/File;
06-08 09:01:09.950: D/dalvikvm(9138): VFY: replacing opcode 0x6e at 0x0007
06-08 09:01:09.950: W/dalvikvm(9138): threadid=11: thread exiting with uncaught exception (group=0x4174c930)
06-08 09:01:09.960: E/AndroidRuntime(9138): FATAL EXCEPTION: AsyncTask #1
06-08 09:01:09.960: E/AndroidRuntime(9138): java.lang.RuntimeException: An error occured while executing doInBackground()
06-08 09:01:09.960: E/AndroidRuntime(9138):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at java.lang.Thread.run(Thread.java:856)
06-08 09:01:09.960: E/AndroidRuntime(9138): Caused by: java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getNoBackupFilesDir
06-08 09:01:09.960: E/AndroidRuntime(9138):     at com.google.android.gms.iid.zzd.zzde(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at com.example.MainActivity$1.doInBackground(MainActivity.java:113)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at com.example.MainActivity$1.doInBackground(MainActivity.java:1)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-08 09:01:09.960: E/AndroidRuntime(9138):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-08 09:01:09.960: E/AndroidRuntime(9138):     ... 4 more

And this is the logcat for an Android 5.1.1:

06-08 09:06:49.786: E/AndroidRuntime(2852): FATAL EXCEPTION: AsyncTask #1
06-08 09:06:49.786: E/AndroidRuntime(2852): Process: com.example, PID: 2852
06-08 09:06:49.786: E/AndroidRuntime(2852): java.lang.RuntimeException: An error occured while executing doInBackground()
06-08 09:06:49.786: E/AndroidRuntime(2852):     at android.os.AsyncTask$3.done(AsyncTask.java:304)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at java.lang.Thread.run(Thread.java:818)
06-08 09:06:49.786: E/AndroidRuntime(2852): Caused by: java.lang.NoSuchMethodError: No virtual method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in /data/app/com.example-1/base.apk)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at com.google.android.gms.iid.zzd.zzde(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at com.google.android.gms.iid.zzd.<init>(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at it.abgsys.htms.MainActivity$1.doInBackground(MainActivity.java:113)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at it.abgsys.htms.MainActivity$1.doInBackground(MainActivity.java:1)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-08 09:06:49.786: E/AndroidRuntime(2852):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-08 09:06:49.786: E/AndroidRuntime(2852):     ... 4 more

At line 113 I have this:

InstanceID instanceID = InstanceID.getInstance(applicationContext);

Used here:

// AsyncTask to register Device in GCM Server
private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";

            InstanceID instanceID = InstanceID.getInstance(applicationContext);
            try {
                regId = instanceID.getToken(GCM_SENDER_ID,
                        GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            if (!TextUtils.isEmpty(regId)) {

                Toast.makeText(
                        applicationContext,
                        "Registered with GCM Server successfully.

"
                                + msg, Toast.LENGTH_SHORT).show();
                Log.e(TAG, "Registered with GCM Server successfully. RegID: " + regId);

            } else { 
                Toast.makeText(
                        applicationContext,
                        "Reg ID Creation Failed.

Either you haven't enabled Internet or GCM server is busy right now. "
                        + "Make sure you enabled Internet and try registering again after some time."
                                + msg, Toast.LENGTH_LONG).show();
                Log.e(TAG, "Reg ID Creation Failed!!!");
            }
        }
    }.execute(null, null, null);
}

I correctly imported the Google Play Services library as described in the documentation.(https://developers.google.com/android/guides/setup)

Has anyone encountered this errors? How can I resolve this?

Development environment used: Eclipse Kepler SR2 with ADT
If you need additional info please comment.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...