在全屏图像视图上添加AdMob后,App Force关闭

时间:2014-08-24 11:09:17

标签: android admob forceclose android-fullscreen

我有一个使用Picasa网络相册的壁纸应用程序,我想添加一些AdMob。我向FullImageActivity和xml添加了一个横幅,当我尝试从应用程序执行FullImageActivity时,应用程序现在强行关闭。我弄清楚了。请帮忙。

FullImageActivity.java

package net.rz.amazinghdwallpapers;

import net.rz.amazinghdwallpapers.app.AppController;
import net.rz.amazinghdwallpapers.picasa.model.Wallpaper;
import net.rz.amazinghdwallpapers.util.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar; 
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.JsonObjectRequest;

import com.google.android.gms.ads.*;

public class FullScreenViewActivity extends Activity implements OnClickListener {
private static final String TAG = FullScreenViewActivity.class
        .getSimpleName();
public static final String TAG_SEL_IMAGE = "selectedImage";
private Wallpaper selectedPhoto;
private ImageView fullImageView;
private LinearLayout llSetWallpaper, llDownloadWallpaper;
private Utils utils;
private ProgressBar pbLoader;

// Picasa JSON response node keys
private static final String TAG_ENTRY = "entry",
        TAG_MEDIA_GROUP = "media$group",
        TAG_MEDIA_CONTENT = "media$content", TAG_IMG_URL = "url",
        TAG_IMG_WIDTH = "width", TAG_IMG_HEIGHT = "height";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen_image);

    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
    llSetWallpaper = (LinearLayout) findViewById(R.id.llSetWallpaper);
    llDownloadWallpaper = (LinearLayout) findViewById(R.id.llDownloadWallpaper);
    pbLoader = (ProgressBar) findViewById(R.id.pbLoader);

    // hide the action bar in fullscreen mode
    getActionBar().hide();

    utils = new Utils(getApplicationContext());

    // layout click listeners
    llSetWallpaper.setOnClickListener(this);
    llDownloadWallpaper.setOnClickListener(this);

    // setting layout buttons alpha/opacity
    llSetWallpaper.getBackground().setAlpha(70);
    llDownloadWallpaper.getBackground().setAlpha(70);

    Intent i = getIntent();
    selectedPhoto = (Wallpaper) i.getSerializableExtra(TAG_SEL_IMAGE);

    // check for selected photo null
    if (selectedPhoto != null) {

        // fetch photo full resolution image by making another json request
        fetchFullResolutionImage();

    } else {
        Toast.makeText(getApplicationContext(),
                getString(R.string.msg_unknown_error), Toast.LENGTH_SHORT)
                .show();
    }
}

/**
 * Fetching image fullresolution json
 * */
private void fetchFullResolutionImage() {
    String url = selectedPhoto.getPhotoJson();

    // show loader before making request
    pbLoader.setVisibility(View.VISIBLE);
    llSetWallpaper.setVisibility(View.GONE);
    llDownloadWallpaper.setVisibility(View.GONE);

    // volley's json obj request
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,
            null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG,
                            "Image full resolution json: "
                                    + response.toString());
                    try {
                        // Parsing the json response
                        JSONObject entry = response
                                .getJSONObject(TAG_ENTRY);

                        JSONArray mediacontentArry = entry.getJSONObject(
                                TAG_MEDIA_GROUP).getJSONArray(
                                TAG_MEDIA_CONTENT);

                        JSONObject mediaObj = (JSONObject) mediacontentArry
                                .get(0);

                        String fullResolutionUrl = mediaObj
                                .getString(TAG_IMG_URL);

                        // image full resolution widht and height
                        final int width = mediaObj.getInt(TAG_IMG_WIDTH);
                        final int height = mediaObj.getInt(TAG_IMG_HEIGHT);

                        Log.d(TAG, "Full resolution image. url: "
                                + fullResolutionUrl + ", w: " + width
                                + ", h: " + height);

                        ImageLoader imageLoader = AppController
                                .getInstance().getImageLoader();

                        // We download image into ImageView instead of
                        // NetworkImageView to have callback methods
                        // Currently NetworkImageView doesn't have callback
                        // methods

                        imageLoader.get(fullResolutionUrl,
                                new ImageListener() {

                                    @Override
                                    public void onErrorResponse(
                                            VolleyError arg0) {
                                        Toast.makeText(
                                                getApplicationContext(),
                                                getString(R.string.msg_wall_fetch_error),
                                                Toast.LENGTH_LONG).show();
                                    }

                                    @Override
                                    public void onResponse(
                                            ImageContainer response,
                                            boolean arg1) {
                                        if (response.getBitmap() != null) {
                                            // load bitmap into imageview
                                            fullImageView
                                                    .setImageBitmap(response
                                                            .getBitmap());
                                            adjustImageAspect(width, height);

                                            // hide loader and show set &
                                            // download buttons
                                            pbLoader.setVisibility(View.GONE);
                                            llSetWallpaper
                                                    .setVisibility(View.VISIBLE);
                                            llDownloadWallpaper
                                                    .setVisibility(View.VISIBLE);
                                        }
                                    }
                                });

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                getString(R.string.msg_unknown_error),
                                Toast.LENGTH_LONG).show();
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Error: " + error.getMessage());
                    // unable to fetch wallpapers
                    // either google username is wrong or
                    // devices doesn't have internet connection
                    Toast.makeText(getApplicationContext(),
                            getString(R.string.msg_wall_fetch_error),
                            Toast.LENGTH_LONG).show();

                }
            });

    // Remove the url from cache
    AppController.getInstance().getRequestQueue().getCache().remove(url);

    // Disable the cache for this url, so that it always fetches updated
    // json
    jsonObjReq.setShouldCache(false);

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}

/**
 * Adjusting the image aspect ration to scroll horizontally, Image height
 * will be screen height, width will be calculated respected to height
 * */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void adjustImageAspect(int bWidth, int bHeight) {
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    if (bWidth == 0 || bHeight == 0)
        return;

    int sHeight = 0;

    if (android.os.Build.VERSION.SDK_INT >= 13) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        sHeight = size.y;
    } else {
        Display display = getWindowManager().getDefaultDisplay();
        sHeight = display.getHeight();
    }

    int new_width = (int) Math.floor((double) bWidth * (double) sHeight
            / (double) bHeight);
    params.width = new_width;
    params.height = sHeight;

    Log.d(TAG, "Fullscreen image new dimensions: w = " + new_width
            + ", h = " + sHeight);

    fullImageView.setLayoutParams(params);
}

/**
 * View click listener
 * */
@Override
public void onClick(View v) {
    Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
            .getBitmap();
    int id = v.getId();
    if (id == R.id.llDownloadWallpaper) {
        utils.saveImageToSDCard(bitmap);
    } else if (id == R.id.llSetWallpaper) {
        utils.setAsWallpaper(bitmap);
    } else {
    }

}
}

activity_fullscreen_image.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="match_parent"
 android:layout_height="match_parent"
 android:background="@color/black" >



<ProgressBar
    android:id="@+id/pbLoader"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" >
</ProgressBar>

<!-- Scroll view for fullscreen preview -->

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >


        <ImageView
            android:id="@+id/imgFullscreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY" />
    </LinearLayout>
</HorizontalScrollView>

<!-- Set as wallpaper button -->

<LinearLayout
    android:id="@+id/llSetWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:background="@drawable/btn_rounded_corner"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@drawable/ico_apply" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="@string/set_wallpaper"
        android:textColor="@color/white"
        android:textSize="18dp" />
</LinearLayout>

<!-- Download wallpaper button -->

<LinearLayout
    android:id="@+id/llDownloadWallpaper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/btn_rounded_corner"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@drawable/ico_download" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="@string/download_wallpaper"
        android:textColor="@color/white"
        android:textSize="18dp" />
</LinearLayout>


<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_centerHorizontal="true"
                     ads:adUnitId="ca-app-pub-1620901891740306/1861775477"
                     ads:adSize="SMART_BANNER"/>

</RelativeLayout>

logcat的:

08-24 11:02:45.863: E/OpenGLRenderer(1388): Getting MAX_TEXTURE_SIZE from GradienCache
08-24 11:02:45.863: E/OpenGLRenderer(1388): MAX_TEXTURE_SIZE: 16384
08-24 11:02:45.879: E/OpenGLRenderer(1388): Getting MAX_TEXTURE_SIZE from  Caches::initConstraints()
08-24 11:02:45.879: E/OpenGLRenderer(1388): MAX_TEXTURE_SIZE: 16384
08-24 11:02:56.615: E/AndroidRuntime(1388): FATAL EXCEPTION: main
08-24 11:02:56.615: E/AndroidRuntime(1388): Process: net.rz.amazinghdwallpapers, PID:   1388
08-24 11:02:56.615: E/AndroidRuntime(1388): java.lang.RuntimeException: Unable to start  activity   ComponentInfo{net.rz.amazinghdwallpapers/net.rz.amazinghdwallpapers.FullScreenViewActivity} : java.lang.ClassCastException: android.widget.ImageView cannot be cast to  com.google.android.gms.ads.AdView
08-24 11:02:56.615: E/AndroidRuntime(1388):     at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.os.Looper.loop(Looper.java:136)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at java.lang.reflect.Method.invoke(Method.java:515)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at dalvik.system.NativeStart.main(Native Method)
08-24 11:02:56.615: E/AndroidRuntime(1388): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.google.android.gms.ads.AdView
08-24 11:02:56.615: E/AndroidRuntime(1388):     at net.rz.amazinghdwallpapers.FullScreenViewActivity.onCreate(FullScreenViewActivity.java:60)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.app.Activity.performCreate(Activity.java:5231)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-24 11:02:56.615: E/AndroidRuntime(1388):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-24 11:02:56.615: E/AndroidRuntime(1388):     ... 11 more

1 个答案:

答案 0 :(得分:0)

您的stacktrace为您提供答案。

您尝试将ImageView投射到AdVIew的FullScreenViewActivity第60行。

我打赌第60行是

AdView adView = (AdView) this.findViewById(R.id.adView);

这意味着在你的布局中,XML R.id.adView实际上是用于ImageView,你需要做一个干净的构建。