I just added adds to my app, but the moment i run it i get a force close from my phone and there is no logcat. I never declared it in my xml but i dont think i need to...
Here's 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();
}
}
and here's 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>
Here's my Manifest:
<?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>
You would be much better off declaring your adView in your XML code. It will prevent weird stuff such as you are seeing from happening. Here's an example of how to declare it in 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>
在某个地方应该会出现错误......我首先看到的是你的main.xml
是一个RelativeLayout
但是你在运行时将它转换为LinearLayout
...改变它以查看它是否有帮助,然后得到回到我们身边
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.