gpt4 book ai didi

java - 无法让 AdMob 工作(使用 Android、LibGDX)

转载 作者:行者123 更新时间:2023-12-02 06:03:59 30 4
gpt4 key购买 nike

我做了很多研究,但没有太多意义,因为我不熟悉放置应用内广告(这是我第一次制作应用程序,也是我第一次使用广告)。

这是我更新的 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>

这是日志猫:

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 外部库导入到您的项目中,here's a link关于如何做到这一点。

关于java - 无法让 AdMob 工作(使用 Android、LibGDX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22439112/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com