点击后停止加载admob广告

时间:2011-11-10 16:56:55

标签: android admob adview

我想在用户点击adview后停止加载广告我已经实现了adlistener并且我放了adView.stopLoading();在onDismissScreen上,但是当我关闭广告网页时出现以下错误:

E/AndroidRuntime(1059): java.lang.RuntimeException: Unable to pause activity {ivn.com.teletextinternational/com.google.ads.AdActivity}: java.lang.NullPointerException

代码:

public class MyActivity extends Activity implements OnClickListener, AdListener {
RelativeLayout relativeLayout1;
 ImageView imgpag; 
 FrameLayout frameLayout1;
 FrameLayout frameLayout2;
 Button right;
 Button left;
 ProgressBar probar;
 LinearLayout linear;
 AdView adView;
 EditText numbox;

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imgpag = (ImageView)findViewById (R.id.imgpag); 
    relativeLayout1 = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    frameLayout1 = (FrameLayout) findViewById(R.id.frameLayout1);
    frameLayout2 = (FrameLayout) findViewById(R.id.frameLayout2);
    left = (Button) findViewById(R.id.left);
    right = (Button) findViewById(R.id.right);
    probar = (ProgressBar) findViewById (R.id.probar);
    adView = new AdView(this, AdSize.BANNER, "a14ebbcd3aae1ff");

    LinearLayout linear = (LinearLayout) findViewById (R.id.linear);
    linear.addView(adView);
    adView.loadAd(new AdRequest());
    adView.setAdListener(this);
 }

public void onDismissScreen(Ad arg0) {
    adView.stopLoading();
            linear.setVisibility(View.GONE);//if you want hide ads
}
}

1 个答案:

答案 0 :(得分:0)

试试这个:

    adView = new AdView(this, AdSize.BANNER, "a14ebbcd3aae1ff");

您创建了一个本地adView对象,而不是初始化您的adView字段。因此,当您尝试调用stopLoading()时,adView为null。