我的应用程序崩溃时启动admob广告

时间:2015-07-13 19:18:23

标签: android admob

你好我们在使用admob广告时遇到问题我在mainfist元数据和活动中获得了权限并且允许每件事情都可以。我的代码在这里是错误的

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;
    AdView mInterstitialAd;

    // 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);
        fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
        llSetWallpaper = (LinearLayout) findViewById(R.id.llSetWallpaper);
        llDownloadWallpaper = (LinearLayout) findViewById(R.id.llDownloadWallpaper);
        pbLoader = (ProgressBar) findViewById(R.id.pbLoader);
        AdView mInterstitialAd = (AdView) findViewById(R.id.adView);
        getActionBar().hide();
        utils = new Utils(getApplicationContext());
        llSetWallpaper.setOnClickListener(this);
        llDownloadWallpaper.setOnClickListener(this);
        llSetWallpaper.getBackground().setAlpha(70);
        llDownloadWallpaper.getBackground().setAlpha(70);
        Intent i = getIntent();
        selectedPhoto = (Wallpaper) i.getSerializableExtra(TAG_SEL_IMAGE);
        AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest);
        if (selectedPhoto != null) {
            fetchFullResolutionImage();
        } else {
            Toast.makeText(getApplicationContext(),
                getString(R.string.msg_unknown_error), Toast.LENGTH_SHORT)
                .show();
        }
    }

private void fetchFullResolutionImage() {
    String url = selectedPhoto.getPhotoJson();
    pbLoader.setVisibility(View.VISIBLE);
    llSetWallpaper.setVisibility(View.GONE);
    llDownloadWallpaper.setVisibility(View.GONE);
    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 {
                        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);
                        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();
                        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();
    switch (v.getId()) {
    // button Download Wallpaper tapped
    case R.id.llDownloadWallpaper:
        utils.saveImageToSDCard(bitmap);
        break;
    // button Set As Wallpaper tapped
    case R.id.llSetWallpaper:
        utils.setAsWallpaper(bitmap);
        break;
    default:
        break;
    }

}

}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
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>


<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" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id" >
        </com.google.android.gms.ads.AdView>

    </LinearLayout>
</HorizontalScrollView>



<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>



<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>
</RelativeLayout>

我的日志

07-13 22:08:21.150: E/dalvikvm(30292): Could not find class 'android.app.AppOpsManager', referenced from method 

com.google.android.gms.common.GooglePlayServicesUtil.zza
07-13 22:08:21.175: E/AndroidRuntime(30292): FATAL EXCEPTION: main
07-13 22:08:21.175: E/AndroidRuntime(30292): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.awesomewallpapers/com.infinityapp.olbuzz.FullScreenViewActivity}: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.os.Looper.loop(Looper.java:153)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.ActivityThread.main(ActivityThread.java:5299)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at java.lang.reflect.Method.invokeNative(Native Method)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at java.lang.reflect.Method.invoke(Method.java:511)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at dalvik.system.NativeStart.main(Native Method)
07-13 22:08:21.175: E/AndroidRuntime(30292): Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.google.android.gms.common.GooglePlayServicesUtil.zzaa(Unknown Source)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.google.android.gms.ads.internal.util.client.zza.zzP(Unknown Source)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.google.android.gms.ads.internal.client.zze.zza(Unknown Source)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.google.android.gms.ads.internal.client.zzy.zzcO(Unknown Source)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.google.android.gms.ads.internal.client.zzy.zza(Unknown Source)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at com.infinityapp.olbuzz.FullScreenViewActivity.onCreate(FullScreenViewActivity.java:74)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.Activity.performCreate(Activity.java:5122)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
07-13 22:08:21.175: E/AndroidRuntime(30292):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)

2 个答案:

答案 0 :(得分:0)

将Google Play服务版本添加到您应用的清单中。请参阅本指南here

编辑应用程序的AndroidManifest.xml文件,并在元素中添加以下声明。这会嵌入应用程序编译的Google Play服务版本。

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

同样在异常中你可以看到上面提到的修复。

答案 1 :(得分:0)

您必须在元素中包含以下声明:

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