java.lang.IllegalStateException。在调用loadAd之前必须设置广告大小和广告单元ID。

问题描述 投票:0回答:1

我正在开发新闻应用程序,我已经实现了带有数据绑定的广告视图,但它给出了以下异常。

java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
     at com.google.android.gms.internal.ads.zzxv.zza(com.google.android.gms:play-services-ads-lite@@19.1.0:82)
     at com.google.android.gms.ads.BaseAdView.loadAd(com.google.android.gms:play-services-ads-lite@@19.1.0:15)
     at com.google.android.gms.ads.AdView.loadAd(com.google.android.gms:play-services-ads-lite@@19.1.0:20)
     at yodgorbek.komilov.musobaqayangiliklari.ui.TopHeadlinesFragment.initializeAds(TopHeadlinesFragment.kt:95)
     at yodgorbek.komilov.musobaqayangiliklari.ui.TopHeadlinesFragment.onViewCreated(TopHeadlinesFragment.kt:56)
     at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:322)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
     at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
     at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
     at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
     at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2167)
     at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1990)
     at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1945)
     at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847)
     at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2621)
     at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
     at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
     at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
     at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1392)
     at android.app.Activity.performStart(Activity.java:7252)
     at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3000)
     at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:185)
     at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:170)
     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:147)
     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858)
     at android.os.Handler.dispatchMessage(Handler.java:106)
     at android.os.Looper.loop(Looper.java:201)
     at android.app.ActivityThread.main(ActivityThread.java:6820)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)

但我已经尝试了以下答案 在调用loadAd之前,必须先设置广告尺寸和广告单位ID。 而这 当程序化设置时,广告尺寸和广告单元ID必须在loadAd之前设置。 还有这个 包括AdView与数据绑定https:/github.comfirebasequickstart-androidblobmasteradmobappsrcmainjavacomgooglesamplesquickstartadmobexamplekotlinMainActivity.kt。 来自StackOverflow,它没有解决我的问题。

下面是我的XML文件,我在这里实现了广告视图。

<data>

    <variable
        name="adId"
        type="String" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pb"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@{adId}"
        ads:layout_constraintTop_toBottomOf="@+id/pb"
        ads:layout_constraintBottom_toBottomOf="@+id/pb"/>


</androidx.constraintlayout.widget.ConstraintLayout>

在我的Fragment类下面

class TopHeadlinesFragment : Fragment() {

    private lateinit var binding: FragmentTopHeadlinesBinding


    private val viewModel by viewModel<MainViewModel>()


    private lateinit var topHeadlinesAdapter: TopHeadlinesAdapter



    //3
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_top_headlines, container, false)
        binding.lifecycleOwner = this
        topHeadlinesAdapter = TopHeadlinesAdapter()
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewModel()
        initializeAds()





    }

    private fun initViewModel() {
        viewModel.sportList.observe(this, Observer { result ->

            when (result) {
                is Results.Success -> {
                    val newList = result.data
                    if (newList != null) {
                        topHeadlinesAdapter.updateData(newList)
                    }
                    binding.recyclerView.adapter = topHeadlinesAdapter
                    topHeadlinesAdapter.notifyDataSetChanged()
                }
            }
        })

        viewModel.showLoading.observe(this, Observer { showLoading ->
            pb.visibility = if (showLoading) View.VISIBLE else View.GONE
        })

        viewModel.loadNews()
    }


    private fun initializeAds(){
        MobileAds.initialize(context)

       // adView.adSize = AdSize.BANNER
        binding.adId = "my unit id"
        val adRequest = AdRequest.Builder().build()
        binding.adView.loadAd(adRequest)
    }

}

我想知道我到底在哪里犯了错误,我已经尝试了所有的StackOverflow,甚至是文档样本,它确实解决了我的问题,我已经尝试了使用kotlin和XML文件以编程方式设置单元ID和大小,它没有工作在所有的任何提示将被感激。

android kotlin adview
1个回答
1
投票
 //Admob banner
        MobileAds.initialize(this, new OnInitializationCompleteListener()  {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                mAdView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                mAdView.setVisibility(View.GONE);
            }


        });

xml

<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_marginTop="10dp"
        android:layout_marginBottom="30dp"
        android:visibility="gone"
        app:adSize="BANNER"
        app:adUnitId="@string/ad_unit_id_banner1" />

体现

 <meta-data
                android:name="com.google.android.gms.ads.APPLICATION_ID"
                android:value="@string/app_id"/>
        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true" />

0
投票

你可以这样写。

val adRequest = AdRequest.Builder().build()
binding.adId = "my unit id"
binding.adView.loadAd(adRequest)
热门问题
推荐问题
最新问题