簡體   English   中英

無法讓AdMob正常工作(使用Android,LibGDX)

[英]Can't get AdMob to work (using Android, LibGDX)

我做了很多研究,但由於我不熟悉放置應用內廣告(這是我第一次制作應用,而且我第一次使用廣告),因此沒有多大意義。

這是我更新的MainActivity.java代碼:

package com.me.mygdxgame;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AndroidApplication {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); //Changed it back to main, got rid of an error

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = true;

        initialize(new MyGame(), cfg);

        AdView adView;
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("My Ad Unit ID (just using this string for posting purposes");

        LinearLayout layout = (LinearLayout) findViewById(R.id.normal); //Changed R.id.linearlayout to R.id.normal. After looking at my R.java class, I saw that there was no id.linearlayout, so I changed it to R.id.normal, which did exist.
        layout.addView(adView); //This is line 44, where the NullPointer is caused

        AdRequest adRequest = new AdRequest.Builder().build();

        adView.loadAd(adRequest);
    }
}

這是更新的AndroidManifest XML代碼:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.me.mygdxgame"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/testing"
        android:label="@string/app_name" >

        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MainActivity"
            android:label="Flappy Nerd"
            android:screenOrientation="portrait"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

    </application>

</manifest>

這是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/normal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

這是logcat:

03-16 16:46:50.126: E/AndroidRuntime(10338): FATAL EXCEPTION: main
03-16 16:46:50.126: E/AndroidRuntime(10338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.mygdxgame/com.me.mygdxgame.MainActivity}: java.lang.NullPointerException
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.os.Looper.loop(Looper.java:137)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.ActivityThread.main(ActivityThread.java:5455)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at java.lang.reflect.Method.invokeNative(Native Method)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at java.lang.reflect.Method.invoke(Method.java:525)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at dalvik.system.NativeStart.main(Native Method)
03-16 16:46:50.126: E/AndroidRuntime(10338): Caused by: java.lang.NullPointerException
03-16 16:46:50.126: E/AndroidRuntime(10338):    at com.me.mygdxgame.MainActivity.onCreate(MainActivity.java:44)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.Activity.performCreate(Activity.java:5372)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-16 16:46:50.126: E/AndroidRuntime(10338):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)

這是一個更新的LogCat:當我拿出似乎導致NullPointers“layout.addView(adView);”的代碼行時,應用程序運行正常,但我從LogCat獲取這些消息:

03-16 17:04:06.723: E/GooglePlayServicesUtil(11199): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

03-16 17:04:09.265: I/Ads(11199): Ad finished loading.

03-16 17:05:09.269: I/Ads(11199): Ad is not visible. Not refreshing ad.

您似乎錯過了Manifest文件中廣告的activity部分:

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

你必須調整你的所有路線。 如果您可以編譯但不起作用,那么您可能正在使用已棄用的AdMob版本。 您必須將google-play_lib外部庫導入到項目中, 這里有一個如何操作的鏈接

你的代碼太亂了!

你應該修復幾個錯誤。 為了避免錯誤你的setContentView(R.layout.main); 必須是super.onCreate(savedInstanceState);之后的第一行super.onCreate(savedInstanceState);

對我來說最令人困惑的是:

為什么你在App的Manifest聲明了RelativeLayout 必須main.xml聲明。 這是NullPointerException的原因。 之所以會發生這種情況,是因為R.id.mainLayout在布局中找不到此ID,因此layout變量為null並且在調用時會導致崩潰(因為它在清單中被錯誤地聲明)。

作為@yugidroid,你的代碼非常混亂! :P但你最終會得到它,別擔心!

最重要的是,不要在你的清單上放置布局,他們不去那里! 在您的文件夾/布局(在項目中)創建一個名為“activity_main.xml”的新xml文件,並用以下內容填充:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

在您的java文件中,更改為

adView.setAdUnitId("xx-xxx-xxx-1234567890123456/1234567890"); //Change the numbers to what google sent you in the email!!!

和Adrequest改為

AdRequest adRequest = new AdRequest.Builder().build();

(不推薦用於測試,但現在它會這樣做)

我建議您再次閱讀文檔,但這非常直截了當https://developers.google.com/mobile-ads-sdk/docs/#play https://developers.google.com/mobile-ads- SDK /文檔/ AdMob廣告/基本面#游戲

我打賭你得到了NullPointerException

findViewById(R.id.normal)

幾乎可以肯定,使用#initialize方法調用#setContentView並用其他東西替換R.layout.main。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM