gpt4 book ai didi

java - 如何解决加载广告时尝试调用虚拟方法的问题

转载 作者:行者123 更新时间:2023-12-02 04:49:19 25 4
gpt4 key购买 nike

我正在 Android 中设置一个应用程序,我想在该应用程序中添加广告。我遵循了以下位置的所有文档:

https://developers.google.com/admob/android/quick-starthttps://developers.google.com/admob/android/banner

但这对我不起作用。

  • 我已经尝试过制作一个新项目并重新开始。
  • 我尝试遵循一些 YouTube 教程。
  • 我尝试重新安装 android studio。
  • 我尝试用谷歌搜索很多东西,但没有任何东西适合我,像这样的东西

How to solve NullPointerException void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest) with Butterknife

这是在我的activity_main.xml中

<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
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="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

我的MainActivity.java

import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {

private Button buttonEnable;
private ImageView imageFlashlight;
private static final int CAMERA_REQUEST = 50;
private boolean flashLightStatus = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MobileAds.initialize(this,
"ca-app-pub-3272193195027774~9744363531");

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

}
}

我的AndroidManifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.myapplication">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-121329314517774~9742163635"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

还有我的 build.gradle(模块:应用程序)

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.android.gms:play-services-ads:17.2.1'
implementation 'com.google.firebase:firebase-ads:17.2.1'
}

错误:

2019-06-05 12:38:40.796 23410-23410/com.example.hp.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hp.myapplication, PID: 23410
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp.myapplication/com.example.hp.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class meta-data
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2858)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1612)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class meta-data
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class meta-data
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.meta-data" on path: DexPathList[[zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/base.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_resources_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/lib/arm64, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.hp.myapplication.MainActivity.onCreate(MainActivity.java:34)
2019-06-05 12:38:40.798 23410-23410/com.example.hp.myapplication E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6982)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2811)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1612)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.example.hp.myapplication-B3wC82OnVOJBCK4nCiFXJA==/split_lib_resources_apk.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:353)
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.<init>(DexFile.java:74)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
at dalvik.system.DexPathList.<init>(DexPathList.java:157)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
at com.android.internal.os.PathClassLoaderFactory.createClassLoader(PathClassLoaderFactory.java:46)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:69)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:36)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:722)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:755)
at android.app.LoadedApk.getResources(LoadedApk.java:982)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2243)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5812)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1680)
... 6 more

最佳答案

您忘记在代码中包含这些行:-

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

您需要膨胀您的 xml 文件(您的布局文件),然后只有您可以调用 findViewById() 方法来查找与您的布局关联的 View 。由于您没有膨胀任何布局,因此 android 无法找到您的 adView 元素,从而显示错误,然后您尝试在其中执行某些操作。

关于java - 如何解决加载广告时尝试调用虚拟方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56456848/

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