java.lang.NoClassDefFoundError的:com.google.android.gms.gcm.GoogleCloudMessaging [英] java.lang.NoClassDefFoundError: com.google.android.gms.gcm.GoogleCloudMessaging

查看:598
本文介绍了java.lang.NoClassDefFoundError的:com.google.android.gms.gcm.GoogleCloudMessaging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发使用GCM示例应用程序<一href="http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/#comment-103785" rel="nofollow">http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/#comment-103785.

I am developing the GCM sample app using http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/#comment-103785.

我无法注册GCM server.I正在以下error.I都瞪大眼睛它。但我无法找到解决方案,所有的解决方案都有关的jar文件我已经做了所有的变化,但我不能得到它。

I am unable to register gcm server.I am getting the following error.I have goggled it .but i couldn't find the solution and all the solutions were about jar files i have done all the changes but i couldn't get it.

错误

FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com.google.android.gms.gcm.GoogleCloudMessaging
    com.sanjeev.integrationapp.RegisterActivity.registerGCM(RegisterActivity.java:80)
    com.sanjeev.integrationapp.RegisterActivity$1.onClick(RegisterActivity.java:47)

    at android.view.View.performClick(View.java:4084)

    at android.view.View$PerformClick.run(View.java:16966)

    at android.os.Handler.handleCallback(Handler.java:615)

    at android.os.Handler.dispatchMessage(Handler.java:92)

    at android.os.Looper.loop(Looper.java:137)

    at android.app.ActivityThread.main(ActivityThread.java:4745)

    at java.lang.reflect.Method.invokeNative(Native Method)

    at java.lang.reflect.Method.invoke(Method.java:511)

    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

    at dalvik.system.NativeStart.main(Native Method)

我的类code如下

public class RegisterActivity extends Activity {

    Button btnGCMRegister;
    Button btnAppShare;
    GoogleCloudMessaging gcm;
    Context context;
    String regId;

    public static final String REG_ID = "regId";
    private static final String APP_VERSION = "appVersion";

    static final String TAG = "Register Activity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    context = getApplicationContext();

    btnGCMRegister = (Button) findViewById(R.id.btnGCMRegister);
    btnGCMRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (TextUtils.isEmpty(regId)) {
                regId = registerGCM();
                Log.d("RegisterActivity", "GCM RegId: " + regId);
            } else {
        Toast.makeText(getApplicationContext(),
            "Already Registered with GCM Server!",
                        Toast.LENGTH_LONG).show();
            }
        }
    });

    btnAppShare = (Button) findViewById(R.id.btnAppShare);
    btnAppShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (TextUtils.isEmpty(regId)) {
        Toast.makeText(getApplicationContext(), "RegId is empty!",
                        Toast.LENGTH_LONG).show();
            } else {
                Intent i = new Intent(getApplicationContext(),
                        MainActivity.class);
                i.putExtra("regId", regId);
                Log.d("RegisterActivity",
            "onClick of Share: Before starting main activity.");
                startActivity(i);
                finish();
    Log.d("RegisterActivity", "onClick of Share: After finish.");
            }
        }
    });
}

public String registerGCM() {

 // below line I am getting error 

gcm = GoogleCloudMessaging.getInstance(this);




    regId = getRegistrationId(context);

    if (TextUtils.isEmpty(regId)) {

        registerInBackground();

    Log.d("RegisterActivity",
    "registerGCM - successfully registered with GCM server - regId: "
                        + regId);
    } else {
        Toast.makeText(getApplicationContext(),
                "RegId already available. RegId: " + regId,
                Toast.LENGTH_LONG).show();
    }
    return regId;
}

@SuppressLint("NewApi")
private String getRegistrationId(Context context) {
    final SharedPreferences prefs = getSharedPreferences(
            MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
    String registrationId = prefs.getString(REG_ID, "");
    if (registrationId.isEmpty()) {
        Log.i(TAG, "Registration not found.");
        return "";
    }
    int registeredVersion = prefs.getInt(APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion) {
        Log.i(TAG, "App version changed.");
        return "";
    }
    return registrationId;
}

private static int getAppVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
Log.d("RegisterActivity","I never expected this! Going down, going down!" + e);
        throw new RuntimeException(e);
    }
}

private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                gcm = GoogleCloudMessaging.getInstance(context);
                }
        regId = gcm.register(Config.GOOGLE_PROJECT_ID);
        Log.d("RegisterActivity", "registerInBackground - regId: "
                        + regId);
        msg = "Device registered, registration ID=" + regId;

                storeRegistrationId(context, regId);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                Log.d("RegisterActivity", "Error: " + msg);
            }
            Log.d("RegisterActivity", "AsyncTask completed: " + msg);
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
             Toast.makeText(getApplicationContext(),
                    "Registered with GCM Server." + msg, 

                   Toast.LENGTH_LONG)
                    .show();
        }
    }.execute(null, null, null);
}

private void storeRegistrationId(Context context, String regId) {
    final SharedPreferences prefs = getSharedPreferences(
            MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
    int appVersion = getAppVersion(context);
    Log.i(TAG, "Saving regId on app version " + appVersion);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(REG_ID, regId);
    editor.putInt(APP_VERSION, appVersion);
    editor.commit();
} 

}

推荐答案

在这里,不需要输入 google_play_services_lib 在工作区中, google_play_services_lib 有一个罐子名为谷歌播放的,services.jar 这需要包括在您的GCM应用程序。

谷歌播放services.jar 包含所有GCM相关的类,这就是为什么 google_play_services_lib 添加为依赖于GCM Android项目。你可以做最简单的事情是添加加入 google_play_services_lib 项目依赖的jar文件放在GCM项目来代替。

要添加罐子在Android项目,你需要做下面的事情,
1,首先创建一个文件夹下你的项目,它是一个文件夹,而不是 LIB
2.然后复制谷歌播放services.jar 里面的文件夹。 3.然后右键点击谷歌播放services.jar ,然后选择构建路径>添加到构建路径

你可以看到 Android的私人图书馆在其中会包含这个jar项目的根文件夹中创建。

注:jar文件的路径 - SDK \演员\谷歌\ google_play_services \ libproject \谷歌播放services_lib \库

Here, no need to import google_play_services_lib in your workspace, google_play_services_lib has a jar named google-play-services.jar which needs to be included in your GCM App.

google-play-services.jar contains all GCM related classes, thats why google_play_services_lib has been added as dependency on GCM Android Project. The simplest thing you can do is to add that jar on your GCM project instead of adding dependency of google_play_services_lib project.

To add jar in android project you have to do following things,
1. First create a libs folder under you project, its a libs folder and not lib.
2. Then copy google-play-services.jar inside libs folder.
3. Then right click on the google-play-services.jar and select Build Path > Add to Build Path.

You can see Android Private Libraries created under your project's root folder which will contain this jar.

Note: Path of jar file - sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs

这篇关于java.lang.NoClassDefFoundError的:com.google.android.gms.gcm.GoogleCloudMessaging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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