旋转设备时出现ClassNotFoundException。将admob库com.google.android.gms:play-services-ads:17.2.0添加到我的项目后,发生了错误。没有这个库,一切都会很好。
我试图将库版本更改为16.0.0,在此版本中不需要应用程序ID,不会发生错误。
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
我的MainActivity.java是
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);
}
}
答案 0 :(得分:0)
旋转时,您的SupportMapFragment被破坏并重新创建。在销毁它之前,它将其当前状态写入一个宗地,以便在重新创建时恢复其状态。片段的保存状态将包括其子视图的状态,并且由于您已在其中嵌套 AdMob视图,因此它也会尝试保存和还原该视图。 AdMob 没有为此所需的内部类SavedState,因此,当尝试从包裹中还原 AdMob 实例时,此过程将失败。
解决方案是,不要在元素内嵌套 AdMob -或任何其他视图。
这是您可以做的,就像黑客一样
@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());
}
答案 1 :(得分:0)
经过漫长的思考。我有答案。 在新库中,添加了具有应用程序ID的ads初始化方法的witch没有内部类SavedState。因此,所有需要的都是像这样保存appid:
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 )
}
类似的问题,但有地图: https://stackoverflow.com/a/36818004/11334749 希望对某人有帮助