【问题标题】:App keeps crashing at start up after I used AdMob使用 AdMob 后,应用程序在启动时一直崩溃
【发布时间】:2017-03-13 00:56:41
【问题描述】:

在我尝试将 横幅广告与 AdMob 结合使用后,我的应用程序不断崩溃(没有它一切正常)。 SplashScreen 加载但应用程序在游戏开始时崩溃。以下是我执行此操作的步骤:

1) 我确定我已安装 Google Play 服务Google 存储库

2) 然后我使用 Firebase Tool Assistant 将我的应用程序连接到 Firebase(它只是在 下添加了 google services.json app 文件夹 在我的项目中)

3)然后我将 AdMob 添加到我的应用中,如下所示:

4)我在布局中添加了以下代码:

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

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

5)在我的名为“Game”的 Main Activity 类中,我将此代码添加到 onCreate() 方法中,如下所示:

public class Game extends Activity {

//ADMOB
private AdView mAdView;

MediaPlayer sound;

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

    //turn title off
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    //set to full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(new GamePanel(this));

    //ADMOB

    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    int[] sounds={R.raw.chiptune, R.raw.chiptune1, R.raw.chiptune2, R.raw.chiptune3};
    Random r = new Random();
    int Low = 0;
    int High = 4;
    int rand = r.nextInt(High-Low) + Low;
    sound = MediaPlayer.create(getApplicationContext(),sounds[rand]);
    sound.start();
    sound.setLooping(true);
}

6) 将此添加到我的字符串中:&lt;string name="banner_ad_unit_id"&gt;ca-app-pub-3940256099942544/6300978111&lt;/string&gt;

7) 在我的 AndroidManifest.xml 中,我添加了以下代码:

<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<!--This meta-data tag is required to use Google Play services.-->
    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

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

如果有人可以提供帮助,我会发现有什么问题!

这是崩溃日志

【问题讨论】:

  • 请查看崩溃日志?
  • 嗨!我添加了崩溃日志。感谢您的帮助!

标签: android admob


【解决方案1】:

findViewById(R.id.adView); 如果 View 存在于您在 setContentView() 中提供的布局中,则返回一个 View,否则返回 null,这就是发生在您身上的事情。

mAdView 为空,因此您会收到空指针异常。

因此,创建一个 layout.xml,嵌入 GamePanel 视图和 AdView,然后将该布局传递给 setContentView();方法。

【讨论】:

  • @MoisoniIonut 在 GamePanel 上显示我的代码,它是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
相关资源
最近更新 更多