Search code examples
androidandroid-asynctasknoclassdeffounderror

App crashing saying java.lang.NoClassDefFoundError : onPostExecute


For old devices my app is crashing and below is the error I am getting.

01-17 09:49:41.080 19462-19462/app.myappname E/AndroidRuntime: FATAL EXCEPTION: main
                                                               java.lang.NoClassDefFoundError: app.myappname.SplashActivity$1$1
                                                                   at app.myappname.SplashActivity$1.onPostExecute(SplashActivity.java:180)
                                                                   at app.myappname.SplashActivity$1.onPostExecute(SplashActivity.java:135)
                                                                   at android.os.AsyncTask.finish(AsyncTask.java:631)
                                                                   at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                   at android.os.Looper.loop(Looper.java:174)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:4952)
                                                                   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:1027)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                                                                   at dalvik.system.NativeStart.main(Native Method)

Any idea what I am doing wrong?


As per research I tried to add below in onCreate of SplashActivity & also in AppController.

try {
  Class.forName("android.os.AsyncTask");
}
catch(Throwable ignore) {
  // ignored
}

Below is what I have in Manifest file.

<application
    android:name=".AppController"
    android:allowBackup="true"
    android:icon="@drawable/app_logo"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme"
    tools:replace="android:icon">


    <activity
        android:name=".SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="adjustResize|stateHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

In Splash activity I am registering the device token for the push notification. Below is what I have for the same. I have this code in onCreate

new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regid;
                Log.i("SplashReg.", "------ >> Registeration msg == " + msg);
                if (!languageSeassionManager.isRegIdToken()) {
                    languageSeassionManager.setRegId(regid);
                    languageSeassionManager.setNotificationStatus(true);
                }

            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {

            // make request to to post the data
            // PostDeviceData postData = new PostDeviceData();
            //  postData.execute();
            Log.e("registeration id ", regid);

            String url = getString(R.string.AddTokenForAndroid);
            String userId = (sessionManager.isLoggedin()) ? sessionManager.getUserCode() : "0";

            String myURL = url
                    + "?token="
                    + regid
                    + "&deviceId="
                    + android_id + "-" + deviceId
                    + "&deviceType=2";

            Log.d("myURL_push", myURL);

            StringRequest req = new StringRequest(Request.Method.GET, myURL
                    ,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    if (!isBackButtonPressed) {
                                        startActivity(new Intent(SplashActivity.this, LanguageActivity.class));
                                        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                                        finish();
                                    }
                                }
                            }, 2000);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("error ", error.getMessage() + "   " + error.toString());

                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            if (!isBackButtonPressed) {
                                startActivity(new Intent(SplashActivity.this, LanguageActivity.class));
                                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                                finish();
                            }
                        }
                    }, 2000);
                }
            });

            // Adding request to request queue
            req.setShouldCache(false);
            req.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, 2, 1.0f));
            req.setTag(TAG);
            AppController.getInstance().addToRequestQueue(req, TAG);


        }
    }.execute(null, null, null);
}

Edit 1

Additional error I found are as below.

01-17 10:11:40.320 531-531/app.myappname E/GooglePlayServicesUtil: The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
01-17 10:11:40.330 531-531/app.myappname I/GCM Demo: Registration not found.
01-17 10:11:40.330 531-531/app.myappname E/dalvikvm: Could not find class 'app.myappname.SplashActivity$1$1', referenced from method app.myappname.SplashActivity$1.onPostExecute

This code works fine on Android OS 5.0 and above.


Solution

  • The error seems to be related not to your SplashActivity itself, but rather to one of classes it is using.

    As I can see from your code these classes can be the cause: GoogleCloudMessaging, StringRequest, and AppController. I think these google classes are not supported in old devices that use older Android API levels