未为 InterstitialAd 类型定义方法 setAdUnitId(String)

Posted

技术标签:

【中文标题】未为 InterstitialAd 类型定义方法 setAdUnitId(String)【英文标题】:The method setAdUnitId(String) is undefined for the type InterstitialAd 【发布时间】:2017-12-21 08:54:21 【问题描述】:

我正在使用导入:import com.google.android.gms.ads.InterstitialAd。; 并且 setADUnitId 错误已经消失,但导入给了我这个错误:导入 com.google.android.gms.ads.InterstitialAd 与另一个导入语句冲突

 import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.backends.android.AndroidApplication;
    import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
    import com.google.ads.Ad;
    import com.google.ads.AdListener;
    import com.google.ads.AdRequest;
    import com.google.ads.AdRequest.ErrorCode;
    import com.google.ads.InterstitialAd;


    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;





    public class MainActivity extends AndroidApplication implements
    AdListener 

        private ActionResolverAndroid actionResolverAndroid;
        private InterstitialAd interstitialAdMob;

        InterstitialAd mInterstitialAd;




        @SuppressLint("HandlerLeak")
        @Override
        public void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            interstitialAdMob = new InterstitialAd(MainActivity.this,"null");
            mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); //The method setAdUnitId(String) is undefined for the type InterstitialAd 
            interstitialAdMob.setAdListener(this);
            interstitialAdMob.loadAd(new AdRequest());
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

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

            actionResolverAndroid = new ActionResolverAndroid(this,
                    startAppInterHandler);
            View v = initializeForView(new JetPig(actionResolverAndroid), cfg);
            setContentView(v);
        

        private boolean startAppExit;


            public Handler startAppInterHandler = new Handler() 

            @Override
            public void handleMessage(Message msg) 
                // if (startAppAd.isReady())
                //
                // 
                switch (msg.what) 
                case 1:
                    if (interstitialAdMob.isReady())
                    interstitialAdMob.show(); // show the ad
                    else
                    interstitialAdMob.loadAd(new AdRequest()); // load the next ad
                    break;
                case 2:
                    if (interstitialAdMob.isReady()) 
                        interstitialAdMob.show();
                        startAppExit = true;
                     else
                        finish();

                default:
                    break;
                
                Gdx.app.log("Add", "In ShowStartApp Hanlder");

                // 
            
        ;

        public boolean isNetworkAvailable() 
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager
                    .getActiveNetworkInfo();
            return activeNetworkInfo != null;
        

        public void finishActivity() 
            finish();
        

        @Override
        public void onStart() 
            super.onStart();
        

        @Override
        public void onStop() 
            super.onStop();
        


        @Override
        public void onDismissScreen(Ad arg0) 
            // TODO Auto-generated method stub
            if (startAppExit) 
                Gdx.app.log("Add", "Finsh");
                finish();
            
            else
            
                interstitialAdMob.loadAd(new AdRequest());
            
        

        @Override
        public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) 
            // TODO Auto-generated method stub

        

        @Override
        public void onLeaveApplication(Ad arg0) 
            // TODO Auto-generated method stub

        

        @Override
        public void onPresentScreen(Ad arg0) 
            // TODO Auto-generated method stub

        

        @Override
        public void onReceiveAd(Ad arg0) 
            // TODO Auto-generated method stub

        
    

【问题讨论】:

【参考方案1】:

在您的代码中您已经导入:com.google.ads.InterstitialAd,那么现在如果您添加导入:com.google.android.gms.ads.InterstitialAd,那么在您的类中将有 2 个名称为 InterstitialAd 的类 => 这会导致碰撞。让我们进入你的代码,你使用InterstitialAd 如下:private InterstitialAd interstitialAdMob; => 编译器怎么知道你想使用什么InterstitialAd? (因为你已经导入了 2 类 InterstitialAd)。因此,如果这种情况下您必须同时使用com.google.ads.InterstitialAdcom.google.android.gms.ads.InterstitialAd,那么您的解决方案将是:导入一个并使用另一个全名。

例如:

import com.google.ads.InterstitialAd;
public class MainActivity extends AndroidApplication implements
    AdListener 
        //variable for class has imported
        private InterstitialAd interstitialAdMob;

        //variable for class has not imported
        private com.google.android.gms.ads.InterstitialAd interstitialAdMob_noImport;
        ...

编辑:对于您的函数setADUnitId,它仅在com.google.android.gms.ads.InterstitialAd 类中可用,所以请删除另一个。

【讨论】:

以上是关于未为 InterstitialAd 类型定义方法 setAdUnitId(String)的主要内容,如果未能解决你的问题,请参考以下文章

Selenium Webdriver:方法 alert() 未为 WebDriver.TargetLocator 类型定义

错误:未为颤振中的“FirebaseMessaging”类型定义“配置”方法

JSP/DAO 错误:未为 ConnectionPool 类型定义方法 getInstance()

getcontentresolver() 未为该类型定义

未为“对象”类型定义运算符“[]”[重复]

未为类型定义运算符“[]”尝试定义运算符“[]”