FATAL和java.lang.RuntimeException:无法启动活动ComponentInfo ERROR

时间:2014-02-20 00:57:32

标签: android admob

当我使用Admob代码我的应用程序是错误的..如果我删除Admob代码我的应用程序是好的...请帮助我...抱歉这个问题没有提供太多信息,我只是不知道什么信息给你(如果这是有道理的?)

我的错误:

02-20 02:46:31.890: E/AndroidRuntime(20007): FATAL EXCEPTION: main
02-20 02:46:31.890: E/AndroidRuntime(20007): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gamecik/com.example.gamecik.MainActivity}: java.lang.IllegalStateException: The ad unit ID can only be set once on AdView.
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.os.Looper.loop(Looper.java:137)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.ActivityThread.main(ActivityThread.java:5450)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at java.lang.reflect.Method.invoke(Method.java:525)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at dalvik.system.NativeStart.main(Native Method)
02-20 02:46:31.890: E/AndroidRuntime(20007): Caused by: java.lang.IllegalStateException: The ad unit ID can only be set once on AdView.
02-20 02:46:31.890: E/AndroidRuntime(20007):    at com.google.android.gms.internal.ag.setAdUnitId(Unknown Source)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at com.google.android.gms.ads.AdView.setAdUnitId(Unknown Source)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at com.example.gamecik.MainActivity.onCreate(MainActivity.java:25)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.Activity.performCreate(Activity.java:5369)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-20 02:46:31.890: E/AndroidRuntime(20007):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
02-20 02:46:31.890: E/AndroidRuntime(20007):    ... 11 more

和我的项目代码:

JAVA:

package com.example.gamecik;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.os.Bundle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

         private MediaPlayer mp;
         private AdView adView;
         private static final String AD_UNIT_ID = "ca-app-pub-1715534499344773/3123443647";

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

      adView = (AdView) findViewById(R.id.adView);
      adView.setAdUnitId(AD_UNIT_ID);
      adView.setAdSize(AdSize.BANNER);

      AdRequest adRequest = new AdRequest.Builder().build();
      adView.loadAd(adRequest);

      // Add click listeners to all the ImageButtons

      View button1 = findViewById(R.id.sescalbuton);
      View button2 = findViewById(R.id.magnumsilah);
      View button3 = findViewById(R.id.Button03); 
      View button4 = findViewById(R.id.Button02);  
      button1.setOnClickListener(this);
      button2.setOnClickListener(this);
      button3.setOnClickListener(this);
      button4.setOnClickListener(this);
   }

   @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;
   }

   // Required method if OnClickListener is implemented

   @Override
   public void onClick(View v) {

      // Find which ImageButton was pressed and take appropriate action

      switch(v.getId()){

         // The cow button
         case R.id.sescalbuton:
             mp = MediaPlayer.create(this, R.raw.ak47);
         break;

         // The duck button
         case R.id.magnumsilah:
             mp = MediaPlayer.create(this, R.raw.magnum);
         break;

         // The sheep button                
         case R.id.Button03:
             mp = MediaPlayer.create(this, R.raw.shotgun);
         break;

         // The uzi               
         case R.id.Button02:
             mp = MediaPlayer.create(this, R.raw.uzi);
         break;

      } 

      mp.seekTo(0);
      mp.start();
   }
}

我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wood"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/sescalbuton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/shotgun"
        android:layout_below="@+id/magnumsilah"
        android:background="@drawable/ak47" />

    <Button
        android:id="@+id/Button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sescalbuton"
        android:layout_centerHorizontal="true"
        android:background="@drawable/sohtgun" />

    <Button
        android:id="@+id/Button02"
        android:layout_width="150dp"
        android:layout_height="110dp"
        android:layout_above="@+id/sescalbuton"
        android:layout_alignRight="@+id/sescalbuton"
        android:background="@drawable/guns" />

     <com.google.android.gms.ads.AdView 
         xmlns:ads="http://schemas.android.com/apk/res-auto"
         android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="ca-app-pub-1715534499344773/3123443647"
                         ads:adSize="BANNER"/>

     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/adView"
         android:layout_alignTop="@+id/adView"
         android:layout_marginTop="28dp"
         android:src="@drawable/supperweapon" />

     <Button
         android:id="@+id/magnumsilah"
         android:layout_width="150dp"
         android:layout_height="100dp"
         android:layout_alignLeft="@+id/imageView1"
         android:layout_below="@+id/imageView1"
         android:background="@drawable/magnum" />

</RelativeLayout>

MYMANİFESTCODE:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gamecik"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

        <activity android:name="com.example.gamecik.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity android:name="com.google.android.gms.ads.AdActivity"  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
<uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

1 个答案:

答案 0 :(得分:0)

我认为您可能希望将广告代码更改为:

    Adview adView = (AdView)this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

你已经拥有XML中的单位ID和大小,看起来其中一个错误就是说它只能设置一次。