我没有错误,广告不会显示(强制关闭)

时间:2014-05-19 04:58:55

标签: java xml admob ads

我创建了一个只有广告的空白应用,并在加载时强制关闭。继承人我做了什么:

将google-play-services库添加到项目中 插入元数据标记 插入权限标记 插入活动标签 在片段xml中插入xmlns行和com.google标记 将com.google导入到主java中 将adview代码添加到主java中 救了它。

没有错误,图形显示屏显示一个框,其中显示了#go;" google"但不显示广告。当我尝试在模拟器中运行它时,应用程序强制关闭。我错过了什么?谢谢你们!

package com.example.abc;

import com.google.android.gms.ads.*;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {


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

        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        AdRequest request = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
        .build();

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }
     @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

好的,谢谢链接工作,我看了代码 我已编辑您的问题以包含代码。 你可以这样试试吗:
而不是简单地导入import com.google.android.gms.ads.*;
尝试使用:

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

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
 adView = new AdView(this); //I couldn't see this line in your code. It goes under the line setContentView(R.layout.activity_main);
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .build();
    adView.loadAd(adRequest);

让我知道会发生什么。另外,您如何在XML中设置广告?