1

I get ClassNotFoundException when i rotate my device. The error occured after I added admob library com.google.android.gms:play-services-ads:17.2.0 to my project. Without this libary everything works fine.

I tried to change the library version to 16.0.0 where the application ID was not necessary, the error does not occure.

2019-04-09 19:32:16.164 12689-12718/com.example.example.myapplication E/Parcel: Class not found when unmarshalling: android.support.v4.app.FragmentManagerState
    java.lang.ClassNotFoundException: android.support.v4.app.FragmentManagerState
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:400)
        at android.os.Parcel.readParcelableCreator(Parcel.java:2564)
        at android.os.Parcel.readParcelable(Parcel.java:2518)
        at android.os.Parcel.readValue(Parcel.java:2421)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:2788)
        at android.os.BaseBundle.unparcel(BaseBundle.java:271)
        at android.os.Bundle.getBundle(Bundle.java:817)
        at fw.onActivityCreated(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):33)
        at com.google.android.gms.measurement.internal.AppMeasurementDynamiteService.onActivityCreated(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):145)
        at cf.a(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):160)
        at k.onTransact(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):4)
        at android.os.Binder.transact(Binder.java:499)
        at com.google.android.gms.internal.measurement.zzq.zza(Unknown Source)
        at com.google.android.gms.internal.measurement.zzdp.onActivityCreated(Unknown Source)
        at com.google.android.gms.internal.measurement.zzex.zzgd(Unknown Source)
        at com.google.android.gms.internal.measurement.zzea$zzb.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:760)
     Caused by: java.lang.ClassNotFoundException: android.support.v4.app.FragmentManagerState
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:1346)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:1406)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.classForName(Native Method) 
        at java.lang.Class.forName(Class.java:400) 
        at android.os.Parcel.readParcelableCreator(Parcel.java:2564) 
        at android.os.Parcel.readParcelable(Parcel.java:2518) 
        at android.os.Parcel.readValue(Parcel.java:2421) 
        at android.os.Parcel.readArrayMapInternal(Parcel.java:2788) 
        at android.os.BaseBundle.unparcel(BaseBundle.java:271) 
        at android.os.Bundle.getBundle(Bundle.java:817) 
        at fw.onActivityCreated(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):33) 
        at com.google.android.gms.measurement.internal.AppMeasurementDynamiteService.onActivityCreated(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):145) 
        at cf.a(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):160) 
        at k.onTransact(:com.google.android.gms.dynamite_measurementdynamite@16089051@16.0.89 (040408-239467275):4) 
        at android.os.Binder.transact(Binder.java:499) 
        at com.google.android.gms.internal.measurement.zzq.zza(Unknown Source) 
        at com.google.android.gms.internal.measurement.zzdp.onActivityCreated(Unknown Source) 
        at com.google.android.gms.internal.measurement.zzex.zzgd(Unknown Source) 
        at com.google.android.gms.internal.measurement.zzea$zzb.run(Unknown Source) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:760) 
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

My MainActivity.java is


public class MainActivity extends AppCompatActivity {
    final Fragment[] fragments = new Fragment[3];
    BottomNavigationView navView;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

        fragmentManager = getSupportFragmentManager();

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getString(R.string.app_name));
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setNavigationIcon(R.drawable.ic_navigation);



        navView = findViewById(R.id.nav_view);


        AdView adView = findViewById(R.id.adView);
        adView.loadAd(new AdRequest.Builder().build());


        fragments[0] = new RandomFragment();
        fragments[1] = new FullListFragment();
        fragments[2] = new FavoriteFragment();

        fragmentManager.beginTransaction().add(R.id.main_container2, fragments[0]).commit();


        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Fragment fragment;

                switch (menuItem.getItemId()) {
                    case R.id.navigation_home:
                        fragment = fragments[0];
                        break;

                    case R.id.navigation_dashboard:
                        fragment = fragments[1];
                        break;


                    case R.id.navigation_notifications:
                        fragment = fragments[2];
                        break;

                    default:
                        fragment = fragments[0];
                        break;
                }
                fragmentManager.beginTransaction().replace(R.id.main_container2, fragment).commit();
                return true;
            }
        });
        navView.setSelectedItemId(R.id.navigation_home);
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("SelectedItemId", navView.getSelectedItemId());

    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int selectedItemId = savedInstanceState.getInt("SelectedItemId");
        navView.setSelectedItemId(selectedItemId);
    }


}

2 Answers2

1

After longest thinkings. I've got answer. In new library added ads initialization method with app id witch does not have an inner class SavedState. So all is need it's save appid like this:

private static final String APP_ID = "your_app_id"

 @Override
    protected void onCreate(Bundle savedInstanceState) {
Bundle adsInstanceState = (savedInstanceState != null)
                ? savedInstanceState.getBundle(APP_ID): null;
        super.onCreate(adsInstanceState);
MobileAds.initialize(this,APP_ID);
}

  @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        Bundle adsInstanceState = new Bundle();
        outState.putBundle(APP_ID, adsInstanceState )
   }

Similar problem but with maps: https://stackoverflow.com/a/36818004/11334749 Hope this help for someone

0

Upon rotation, your SupportMapFragment gets destroyed and recreated. Before it's destroyed, it writes its current state to a Parcel, to be used in restoring its state when recreated. The Fragment's saved state will include the state of its child Views, and since you've nested a AdMob View within it, it attempts to save and restore that, as well. The AdMob does not have an inner class SavedState necessary for that, so this process fails when trying to restore the AdMob instance from the Parcel.

The solution is to not nest the AdMob- or any other View, for that matter - within the element.

This is something you can do, it is just like and hack

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    AdView adView = (AdView)findViewById(R.id.adView);
    View contentFrame = findViewById(R.id.contentFrameLayout);

    RelativeLayout parent = (RelativeLayout) adView.getParent();
    LayoutParams mapFrameParams = contentFrame.getLayoutParams();
    LayoutParams adViewParams = adView.getLayoutParams();

    parent.removeView(adView);
    parent.removeView(contentFrame);

    AdView newAdView = new AdView(this, AdSize.SMART_BANNER, getString(R.string.admob_pubId));

    newAdView.setId(R.id.adView);

    parent.addView(newAdView, adViewParams);
    parent.addView(contentFrame,mapFrameParams);
    newAdView.loadAd(new AdRequest());
}
Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
  • 1
    I didn't add any AdView yet. However I registered app id in manifest like this `` which is required by the admob library. After adding this to manifest I have the above error. – Daniel Siaras Apr 10 '19 at 13:29
  • Can you try some thing like this in fragment code Override public void onResume() { super.onResume(); if (newAdView!= null) { AdRequest adRequest = new AdRequest.Builder().build(); newAdView.loadAd(adRequest); } } Override public void onPause() { newAdView.pause(); super.onPause(); } Override public void onDestroy() { newAdView.destroy(); super.onDestroy(); } – Ankit Tale Apr 10 '19 at 17:46