应用程序是强制关闭(Admob)?

时间:2012-03-28 00:54:19

标签: java android xml admob

我刚刚添加了添加到我的应用程序,但是当我运行它的那一刻,我从手机关闭了一个力量,没有logcat。我从未在我的xml中声明它,但我认为我不需要......

这是admob.java:

package isaac.n;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
import com.google.ads.*;


public class admob extends Activity {
      private AdView adView;

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

        // Create the adView
        adView = new AdView(this, AdSize.BANNER,  "a14f725ebadc0f2");

        // Lookup your LinearLayout assuming it’s been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout)findViewById(R.layout.main);

        // Add the adView to it
        layout.addView(adView);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());
      }

      @Override
      public void onDestroy() {
        if (adView != null) {
          adView.destroy();
        }
        super.onDestroy();
      }
    }

这里是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gray3" >

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="163dp"
        android:layout_height="163dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dp"
        android:src="@drawable/calc" />


    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/red"
        android:text="@string/Board"
        android:textColor="@color/white"
        android:textSize="30px"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button10"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignLeft="@+id/imageView3"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="50dp"
        android:background="@null" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="190dp"
        android:layout_height="190dp"
        android:layout_below="@+id/imageView3"
        android:layout_marginTop="33dp"
        android:layout_toLeftOf="@+id/imageView3"
        android:maxHeight="150dp"
        android:maxWidth="150dp"
        android:src="@drawable/white1" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_alignBottom="@+id/imageView4"
        android:layout_alignLeft="@+id/button10"
        android:layout_marginBottom="28dp"
        android:src="@drawable/remin1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_alignBottom="@+id/imageView3"
        android:layout_alignRight="@+id/imageView4"
        android:layout_marginBottom="18dp"
        android:layout_marginRight="16dp"
        android:src="@drawable/notes" />

    <Button
        android:id="@+id/button1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignRight="@+id/imageView2"
        android:layout_alignTop="@+id/imageView2"
        android:background="@null" />

    <Button
        android:id="@+id/button2"
        android:layout_width="170px"
        android:layout_height="170px"
        android:layout_alignRight="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        android:background="@null" />

    <Button
        android:id="@+id/button4"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignRight="@+id/imageView2"
        android:background="@null" />

</RelativeLayout>

这是我的清单:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="isaac.n"
    android:versionCode="2"
    android:versionName="1.1" >

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


     <uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application



        android:icon="@drawable/ic_launcher1"
        android:label="@string/app_name" >

        <activity android:name="com.admob.android.ads.admob"/>
<activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation"/>


        <activity
            android:name="isaac.n.IzzynActivity"
            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="isaac.n.notes"
            android:label="@string/notes"
            android:screenOrientation="portrait"></activity>

           <activity android:name="isaac.n.FingerPaint"
                  android:label="WhiteBoard"
                  android:screenOrientation="portrait"></activity>

        <activity 
            android:name="isaac.n.calculator"
            android:label="@string/calculator"
            android:screenOrientation="portrait"></activity>


        <activity android:name=".main1"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
    </application>








</manifest>

2 个答案:

答案 0 :(得分:2)

在XML代码中声明adView会更好。它可以防止你看到的奇怪东西发生。这是一个如何用XML声明它的例子。

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:orientation="vertical" style="@style/background">
  <com.google.ads.AdView android:id="@+id/adView" ads:loadAdOnCreate="true" ads:adSize="BANNER" ads:adUnitId="@string/admob_unit_id" android:layout_width="wrap_content" android:layout_height="wrap_content" ></com.google.ads.AdView>
 </RelativeLayout>

答案 1 :(得分:1)

某处应该会出现错误...我看到的第一件事是你的main.xmlRelativeLayout,但你在运行时将其投放到LinearLayout ...改变它以确定它是否有帮助,然后回到我们身边。