gpt4 book ai didi

java - Admob 6.4.1 - 膨胀类 com.google.ads.AdView 时出错

转载 作者:行者123 更新时间:2023-12-01 14:18:18 25 4
gpt4 key购买 nike

将 admob 添加到我的 xml 时,会导致 fatal error ,从而导致应用程序崩溃。

我在/libs 中正确添加了 admob jar 并构建路径。

xml 看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >

<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="#######"
ads:adSize="BANNER"
ads:testDevices="######"
ads:loadAdOnCreate="true"/> </RelativeLayout>

这里是logcat

07-29 00:39:59.113: E/Ads(8849): Could not initialize AdView: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.113: E/Ads(8849): Could not initialize AdView: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.113: W/dalvikvm(8849): threadid=1: thread exiting with uncaught exception (group=0x40da9390) 07-29 00:39:59.133: E/AndroidRuntime(8849): FATAL EXCEPTION: main 07-29 00:39:59.133: E/AndroidRuntime(8849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clickvote.app/com.clickvote.app.WebViewActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.ads.AdView 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.access$600(ActivityThread.java:151) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.os.Handler.dispatchMessage(Handler.java:99) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.os.Looper.loop(Looper.java:155) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.main(ActivityThread.java:5511) 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Method.invokeNative(Native Method) 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Method.invoke(Method.java:511) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 07-29 00:39:59.133: E/AndroidRuntime(8849): at dalvik.system.NativeStart.main(Native Method) 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.ads.AdView 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.createView(LayoutInflater.java:613) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl.java:310) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.clickvote.app.WebViewActivity.onCreate(WebViewActivity.java:45) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.Activity.performCreate(Activity.java:5066) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 11 more 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: java.lang.reflect.InvocationTargetException 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Constructor.constructNative(Native Method) 07-29 00:39:59.133: E/AndroidRuntime(8849): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 07-29 00:39:59.133: E/AndroidRuntime(8849): at android.view.LayoutInflater.createView(LayoutInflater.java:587) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 20 more 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: java.lang.RuntimeException: Could not initialize AdView: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.google.ads.AdView.a(SourceFile:402) 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.google.ads.AdView.(SourceFile:125) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 23 more 07-29 00:39:59.133: E/AndroidRuntime(8849): Caused by: com.google.ads.internal.b: AdView was initialized with a Context that wasn't an Activity. 07-29 00:39:59.133: E/AndroidRuntime(8849): at com.google.ads.AdView.a(SourceFile:389) 07-29 00:39:59.133: E/AndroidRuntime(8849): ... 24 more

最佳答案

好的,根据您的评论,我将发布之前的建议作为带有一些示例代码的答案,因为正如您所指出的,您对 Android 还很陌生。

您当前的代码:

public class WebViewActivity extends DroidGap {
public static String LOG_TAG = "name";
private Dialog mLoadingDialog;

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setCustomView(R.layout.ab); // <-- !!
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
}
}

基本上,您应该尝试重写上面指出的行。不要将自定义 View 膨胀委托(delegate)给 ActionBar,而是自行膨胀以确保使用 Activity 上下文。就像这样:

public class WebViewActivity extends DroidGap {
public static String LOG_TAG = "name";
private Dialog mLoadingDialog;

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View customActionBarView = getLayoutInflater().inflate(R.layout.ab, null, false);
getActionBar().setCustomView(customActionBarView);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
}
}

这应该(可能)消除您当前看到的异常。我没有验证或尝试运行上面的代码 fragment ,因此请注意任何拼写错误。如果您在此之后仍然遇到同样的问题,请轻推我,我会仔细看看。

关于java - Admob 6.4.1 - 膨胀类 com.google.ads.AdView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914355/

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