简体   繁体   English

Android Studio 广告引发错误

[英]Android Studio Ads Throw Errors

I have been trying to implement ads into my android studio project, but whenever I try and run my app it crashes before showing anything likely due to the OnStart function.我一直在尝试在我的 android 工作室项目中实施广告,但是每当我尝试运行我的应用程序时,它在显示任何可能由于 OnStart function 导致的任何内容之前都会崩溃。 The code is:代码是:

private AdView mAdView = findViewById(R.id.adView1);
public String Input;
public search search = new search();



@Override
protected void onCreate(Bundle savedInstanceState)
{

    getSupportActionBar().hide();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });
    
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);



}

The app runs when I remove the private AdView mAdView line from the start but no ads are shown.当我从一开始删除私有 AdView mAdView 行但没有显示广告时,该应用程序运行。

My XML is:我的 XML 是:

<com.google.android.gms.ads.AdView
    android:id="@+id/adView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginStart="53dp"
    android:layout_marginTop="420dp"
    app:adSize="BANNER"
    app:adUnitId="ca-app-pub-3940256099942544/6300978111"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"></com.google.android.gms.ads.AdView>

The error code that is thrown when the app is run is:应用程序运行时抛出的错误代码是:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firstever, PID: 10185
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.firstever/com.example.firstever.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
    at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:173)
    at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
    at android.content.Context.obtainStyledAttributes(Context.java:744)
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
    at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
    at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
    at com.example.firstever.MainActivity.<init>(MainActivity.java:24)
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

check out your manifest if you add this code如果您添加此代码,请查看您的清单

 ...<application ...  > ...
 < meta-data 
   android:name="com.google.android.gms.ads.APPLICATION_ID"
   android:value="ad_user_id_here_from_admob" /> 
...</application>...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM