使用Google Admob时,我的应用程序崩溃了

时间:2016-11-14 21:49:16

标签: java android nullpointerexception admob

我试图在mainActivity中展示广告,并且我已经从这里复制了代码 - > https://firebase.google.com/docs/admob/android/targeting#test_ads 但是使用我自己的凭证。

我知道这里还有其他的ppl具有相同的' null对象引用'错误但他们的解决方案似乎无法解决问题。 该应用会在adView.loadAd被注释掉时运行。

MainActivity

public class MainActivity extends AppCompatActivity {
    static MainActivity ma = new MainActivity();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MobileAds.initialize(getApplicationContext(), "ca-app-pub-xxxxxxxxxxxx/xxxxxxx");

        AdView adView = (AdView)findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder().build();
                //.addTestDevice("Device ID")        // All emulators

        adView.loadAd(adRequest);

        Music.playMusic(this.getApplicationContext(), 1, 0, 3, 0);

        // Display the Main Activity.
        setContentView(R.layout.activity_main);
        highscore.highscoreSetorGet(this.getApplicationContext(), 0, 0, 0, 0, false);
        displayHighscore();
    }
}

的AndroidManifest.xml

<activity
    android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    android:theme="@android:style/Theme.Translucent" />

activity_main.xml中

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admob_id"/>
</RelativeLayout>

string.xml

<resources>
    <string name="admob_id">ca-app-pub-xxxxxxxxxxxx/xxxxxxx</string>
</resources>

错误...

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.sam.myfirstapp, PID: 2362
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sam.myfirstapp/com.example.sam.myfirstapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
                  at com.example.sam.myfirstapp.MainActivity.onCreate(MainActivity.java:28)
                  at android.app.Activity.performCreate(Activity.java:6664)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

申请已终止。

0 个答案:

没有答案