Admob崩溃的应用程序

时间:2014-06-15 03:34:41

标签: android admob

我有一个应用程序,我尝试插入admob,但在插入Admob代码后,我的应用程序崩溃,它说' 不幸的是,我的应用已停止。你能告诉我哪里可能出错了吗?这是代码。

public class MainActivity extends BaseActivity {

    /**
     * The intent that will start the list activity
     */
    private Intent activityIntent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // set the layout
        setContentView(R.layout.activity_main);
        // create the activity that will start the list
        activityIntent = new Intent(mcontext, EntryListActivity.class);
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("TEST_DEVICE_ID")
            .build();
        adView.loadAd(adRequest);
        // check whether we need to set the default values on the preferences
        if (!preferencesRepository.isDefaultPreferencesSet()) {
            PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {

        super.onResume();

        // initiate Pollfish (see http://www.pollfish.com)
        PollFish.init(this, AppConstants.POLLFISH_API_KEY, Position.TOP_LEFT, 30);

        AppUtil.initiateDataCheckService(mcontext);
    }

    /**
     * Starts the @EntryListActivity on the emergency tab
     * 
     * @param caller
     */
    public void showEntryList(View caller) {

        String category = "";

        switch (caller.getId()) {
            case R.id.menu_info:
                category = mcontext.getString(R.string.CAT_NAME_INFORMATION);
                break;
            case R.id.menu_transportation:
                category = mcontext.getString(R.string.CAT_NAME_TRANSPORTATION);
                break;
            case R.id.menu_embassies:
                category = mcontext.getString(R.string.CAT_NAME_EMBASSIES);
                break;
            case R.id.menu_public_services:
                category = mcontext.getString(R.string.CAT_NAME_PUBLIC_SERVICES);
                break;
            case R.id.menu_helplines:
                category = mcontext.getString(R.string.CAT_NAME_HELPLINES);
                break;
            case R.id.menu_police:
                category = mcontext.getString(R.string.CAT_NAME_POLICE);
                break;
            case R.id.menu_healthcare:
                category = mcontext.getString(R.string.CAT_NAME_HEALTHCARE);
                break;
            case R.id.menu_emergency:
                category = mcontext.getString(R.string.CAT_NAME_EMERGENCY);
                break;
            case R.id.menu_user:
                category = mcontext.getString(R.string.CAT_NAME_USER_DEFINED);
                break;

        }

        // send the tapped category to the @EntryListActivity
        activityIntent.putExtra(EntryListActivity.PARAM_CATEGORY, category);

        // start the activity
        startActivity(activityIntent);

    }

}

这里是.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
style="@style/activity_theme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="0dp" >

<ScrollView
    android:id="@+id/main"
    style="@style/app_container_large_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="vertical"
    android:paddingBottom="0dp" >

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/main_grid" />
</ScrollView>

<include layout="@layout/header" />

<ImageView
    android:id="@+id/shadow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/main"
    android:layout_gravity="center_horizontal"
    android:contentDescription="@string/ΤΧΤ_EMPTY"
    android:gravity="center_horizontal"
    android:src="@drawable/bottom_shadow" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_alignParentTop="true"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adUnitId=""
        ads:adSize="BANNER"
        android:background="#ECE5E4"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

可能是名称空间问题。您必须定义命名空间 您已错误地配置了XML命名空间。它应该是:

xmlns:ads="http://schemas.android.com/apk/res-auto"

如果您使用的是 Google Play服务版的Admob

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads

`如果您使用的是 Admob-6.4.1或更早版本,它看起来就像您一样。

即。只需从

更改您的xml Relativelayout即可
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android";
android:id="@+id/container" 
style="@style/activity_theme" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center_horizontal" 
android:orientation="vertical" 
android:padding="0dp" >

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/container" 
    style="@style/activity_theme" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    android:padding="0dp" >

来自xmlns:ads="schemas.android.com/apk/res-auto";

的卸妆<com.google.ads.AdView