我正在制作一个脚蹼图像应用程序,我的应用程序正常工作,没有广告,但在添加admob广告后,我的应用程序没有启动,它崩溃。 我怎么解决它?任何帮助将不胜感激
我的Java代码:
public class MainActivity extends Activity {
private AdView adView;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private ViewFlipper mViewFlipper;
private AnimationListener mAnimationListener;
private Context mContext;
@SuppressWarnings("deprecation")
private final GestureDetector detector = new GestureDetector(new SwipeGestureDetector());
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adView
adView = new AdView(this, AdSize.BANNER, "12345678910123");
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="@+id/mainLayout"
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
// Add the adView to it
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
mContext = this;
mViewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
mViewFlipper.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
findViewById(R.id.play).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//sets auto flipping
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(4000);
mViewFlipper.startFlipping();
}
});
findViewById(R.id.stop).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//stop auto flipping
mViewFlipper.stopFlipping();
}
});
//animation listener
mAnimationListener = new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
//animation started event
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
//TODO animation stopped event
}
};
}
class SwipeGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.left_out));
// controlling animation
mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper.showNext();
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.right_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.right_out));
// controlling animation
mViewFlipper.getInAnimation().setAnimationListener(mAnimationListener);
mViewFlipper.showPrevious();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
}
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/p1" />
<TextView
android:id="@+id/textView1"
style="@style/ImageTitle"
android:text="@string/text1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/p2" />
<TextView
style="@style/ImageTitle"
android:text="@string/text2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/p3" />
<TextView
style="@style/ImageTitle"
android:text="@string/text3" />
</RelativeLayout>
</ViewFlipper>
<LinearLayout
style="@style/ButtonContainer"
android:orientation="horizontal" >
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@android:drawable/ic_media_play" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/ic_media_pause" />
</LinearLayout>
<ImageView
android:id="@+id/swipe_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/back" />
<ImageView
android:id="@+id/swipe_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/next" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="376dp"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="12345678910123"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
</LinearLayout>
</RelativeLayout>