我已经在android studio中开发了一个应用程序,并且尝试在屏幕底部添加横幅。我曾尝试将横幅广告放在单独的活动中,但随后它没有显示在屏幕上。然后,我认为我必须放入屏幕上显示的活动中,因此我尝试将与横幅相关的代码放入“主要活动”中,然后在我运行时应用程序立即崩溃。
我不知道问题出在哪里,因为我遵循了开发人员的Google指南和一些youtube教程。任何帮助或建议表示赞赏。
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string> //in strings.xml
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<uses-permission android:name="android.permission.INTERNET"></uses- permission> // AndroidManifest.xml. Also have user permissions here.
public class MainActivity extends AppCompatActivity {
private static final String MAIN_ACTIVITY_TAG = "Application";
AdView mAdView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest); // in activity along with a number of imports necessary and the rest of the application. Also tried this in admob banner activity
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView> //in activity_main.xml. Also tried this in admob banner xml.
这是Logcat:
2019-02-04 17:35:12.430 6334-6334/com.uberspot.a2048 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.uberspot.a2048, PID: 6334
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uberspot.aApplication/com.uberspot.aApplication.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:555)
at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
at android.support.v7.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:403)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.uberspot.a2048.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
作为例外情况,您应使用Theme.Appcompat
或后代。
因此,请确保您的AppTheme
父母在AppCompat
中是styles.xml
。
例如:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>