0

building a app that will push notification enabled.when i run it on real device app crashes on start let me show u guys my code. MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.SharedPreferences;
import static com.my.app.CommonUtilities.SENDER_ID;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gcm.GCMRegistrar;
import com.my.app.R;
    public class MainActivity extends Activity {
        private String TAG = "** pushAndroidActivity **";
        private TextView mDisplay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    checkNotNull(SENDER_ID, "SENDER_ID");
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    setContentView(R.layout.activity_main);
    mDisplay = (TextView) findViewById(R.id.display);
    final String regId = GCMRegistrar.getRegistrationId(this);
    Log.i(TAG, "registration id =====  "+regId);
    if (regId.equals("")) {
    GCMRegistrar.register(this, SENDER_ID);
    } else {
    Log.v(TAG, "Already registered");
    }
    mDisplay.setText("ffffff"+regId);
    }

and outside onCreate i have

private void checkNotNull(Object reference, String name) {
        if (reference == null) {
        throw new NullPointerException(
        getString(R.string.error_config, name));
        }
        }

        @Override
        protected void onPause() {
        super.onPause();
        GCMRegistrar.unregister(this);
        }

but when i compile and run it on device app suddenly crashes before even loading MainActivity let me post LogCat here.

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gcm.GCMRegistrar
com.my.app.MainActivity.onCreate(MainActivity.java:28)
android.app.Activity.performCreate(Activity.java:5008)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
android.app.ActivityThread.access$600(ActivityThread.java:130)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4745)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
dalvik.system.NativeStart.main(Native Method)

and my Manifest.xml

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="com.my.app.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
 <receiver android:name=".MyC2dmReceiver" android:permission="com.google.android.c2dm.permission.SEND">
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="com.my.app" />
          </intent-filter>
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
              <category android:name="com.my.app" />
          </intent-filter>
      </receiver>
user2966615
  • 373
  • 4
  • 16
  • Are you using Library project? If yes then must be sure that it is indicating with green mark. And if you are using jar file then it must be in **libs** folder. And finally all is perfect then clean your project and rebuild it. – Piyush Jan 24 '14 at 09:02
  • take a look here http://stackoverflow.com/questions/11273938/gcm-java-lang-noclassdeffounderror-com-google-android-gcm-gcmregistrar – Damien R. Jan 24 '14 at 09:04
  • check: [this](http://stackoverflow.com/questions/11273938/gcm-java-lang-noclassdeffounderror-com-google-android-gcm-gcmregistrar), [this](http://stackoverflow.com/questions/12734303/gcm-java-lang-noclassdeffounderror-com-google-android-gcm-gcmregistrar),[this](http://stackoverflow.com/questions/16824143/java-lang-noclassdeffounderror-com-google-android-gcm-gcmregistrar) and [this](http://stackoverflow.com/questions/15055351/getting-noclassdeffounderror-with-google-cloud-messaging-gcm-on-nexus-4). – Paresh Mayani Jan 24 '14 at 09:05
  • yess my gcm.jar isnt in libs. how can i copy it to libs? – user2966615 Jan 24 '14 at 09:16

2 Answers2

1

This is due to it unable to find the jar file. Have a look at the below solutions :

GCM : java.lang.noclassdeffounderror: com.google.android.gcm.GCMRegistrar

java.lang.NoClassDefFoundError: com.google.android.gcm.GCMRegistrar

GCM NoClassDefFoundError when checking device/manifest

Community
  • 1
  • 1
Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35
0

From the logcat output, it is evident that Android couldn' t find GCMRegistrar class, which is either in library project, or in GCM.jar. In Android projects you need to put jar files inside libs folder, then they will automatically be included to your classpath. If you are using GCM.jar library, you need to copy it under libs folder. If libs folder does not exist, you can create it by right clicking on project.

Alpay
  • 1,350
  • 2
  • 24
  • 56
  • i coppied gcm.jar to my libs folder and also added it to build path but still app crashes – user2966615 Jan 24 '14 at 09:29
  • Please view Manifest file and do let me know if i am missing somthing. Thanks – user2966615 Jan 24 '14 at 09:39
  • [This](http://developer.android.com/google/gcm/client.html) document explains everything in detail. It also contains [this](https://code.google.com/p/gcm/source/browse/#git%2Fgcm-client) client sample. You can check if your manifest and other parts right by looking the example. – Alpay Jan 24 '14 at 09:46
  • ya i checked and everything looks fine. but app still crashes – user2966615 Jan 24 '14 at 09:57
  • i have downloaded and installed this example from google and this app does the same. means crashes before even start! what could be the reason now?? my phone? or anything else? – user2966615 Jan 24 '14 at 10:08
  • Have you updated google play services on the device? Can you test this program with a recent Android device? Check the logcat output to see if the error is the same class-not-found error. – Alpay Jan 24 '14 at 11:43