在下面的错误报告中,它说找不到片段“com.android.myproject.ad” - 这对我来说非常困惑,因为项目中没有“广告”类或对象。唯一的原因可能是AdMob库/ SDK。
有没有其他人在AdMob上看过这个? 我查看了我正在使用的AdMob SDK的版本,它是最新的(第8版)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.myproject/com.android.myproject.ActivityMain}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.myproject.ad: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$2500(ActivityThread.java:129)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2117)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4717)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.android.myproject.ad: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Unknown Source)
at android.support.v4.app.FragmentState.instantiate(Unknown Source)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(Unknown Source)
at android.support.v4.app.FragmentActivity.onCreate(Unknown Source)
at com.android.myproject.ActivityMain.onCreate(Unknown Source)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2717)
... 11 more
Caused by: java.lang.InstantiationException: com.android.myproject.ad
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1429)
通过XML布局显示AdMob横幅:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- widgets like buttons and stuff -->
</LinearLayout>
<RelativeLayout
android:id="@+id/bannersLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<com.google.ads.AdView
android:id="@+id/adViewBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adUnitId="@string/admob_id"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>
为了保护我的应用免受这个讨厌的问题,是否有更好的方式来展示AdMob广告(如果AdMob甚至是问题)。