Android不幸的应用已经停止了admob

时间:2014-06-19 12:35:38

标签: android admob

我正在尝试在我的Android应用程序中添加adMob广告,但它让我遗憾地停止了错误,

我不是android程序的专家所以不知道什么是错误的。

下面是我的MainActivity, adview, and manifest

MainActivity

package com.nyt.ilm.ilmsarf;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.nyt.ilm.ilmsarf.P1.OnFragmentInteractionListener;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.ListView;
import android.widget.RelativeLayout;


public class MainActivity extends FragmentActivity implements
        OnFragmentInteractionListener {

    ViewPager viewpager;
    Context mContext=MainActivity.this; 
    SharedPreferences appPreferences; 
    boolean isAppInstalled = false; 
     /* Your ad unit id. Replace with your actual ad unit id. */
     public boolean adViewHeightSet = false;
        @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);

        appPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
        isAppInstalled = appPreferences.getBoolean("isAppInstalled",false); 
        if(isAppInstalled==false){ 
               // add shortcutIcon code here
            Intent shortcutIntent = new Intent(getApplicationContext(),
                    MainActivity.class);

            shortcutIntent.setAction(Intent.ACTION_MAIN);
            //shortcutIntent is added with addIntent
            Intent addIntent = new Intent();
          addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "IlmSarf");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                            R.drawable.ic_launcher));

            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
            // finally broadcast the new Intent
            getApplicationContext().sendBroadcast(addIntent);
            // finally isAppInstalled should be true. 
        SharedPreferences.Editor editor = appPreferences.edit(); 
        editor.putBoolean("isAppInstalled", true); 
        editor.commit(); 
        }

        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        FragmentPagerAdapter fm = new MyPageAdapter(getSupportFragmentManager());
        viewpager.setAdapter(fm);
        viewpager.setCurrentItem(fm.getCount() - 1);

        //// Ad Banner

        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        adView.getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (adViewHeightSet) return;

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

                ListView list = (ListView) findViewById(R.id.list);                

                RelativeLayout.LayoutParams params = 
                new RelativeLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,      
                        LayoutParams.MATCH_PARENT
                );

                params.setMargins(0, 0, 0,   adView.getHeight()+ 15);

                list.setLayoutParams(params);
                adViewHeightSet = true;
            }
        });


    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        // TODO Auto-generated method stub

    }

}

class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
    public MyPageAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new P1());
        fragments.add(new P2());
        fragments.add(new P3());  

    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return fragments.size();
    }

}

adview.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:dividerHeight="0dp">
    </ListView>    
</RelativeLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.nyt.ilm.ilmsarf"
    android:versionCode="3"
    android:versionName="1.3" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <uses-permission android:name='com.android.launcher.permission.UNINSTALL_SHORTCUT'/>
<uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
    <activity android:label="@string/app_name" android:name="adview">
        </activity>
        <activity
            android:name="com.nyt.ilm.ilmsarf.MainActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"       />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

这里是logcat

06-19 17:19:05.452: I/Process(1144): Sending signal. PID: 1144 SIG: 9
06-19 17:19:49.687: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.687: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.687: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.687: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.687: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.687: D/jdwp(1237): sendBufferedRequest : len=0x41
06-19 17:19:49.698: D/dalvikvm(1237): open_cached_dex_file : /data/app/com.nyt.ilm.ilmsarf-1.apk /data/dalvik-cache/data@app@com.nyt.ilm.ilmsarf-1.apk@classes.dex
06-19 17:19:49.699: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.699: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.699: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.699: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.705: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.705: W/Trace(1237): Unexpected value from nativeGetEnabledTags: 0
06-19 17:19:49.706: D/dalvikvm(1237): create interp thread : stack size=32KB
06-19 17:19:49.706: D/dalvikvm(1237): create new thread
06-19 17:19:49.706: D/dalvikvm(1237): new thread created
06-19 17:19:49.706: D/dalvikvm(1237): update thread list
06-19 17:19:49.706: D/dalvikvm(1237): threadid=11: interp stack at 0x5d824000
06-19 17:19:49.706: D/dalvikvm(1237): threadid=11: created from interp
06-19 17:19:49.706: D/dalvikvm(1237): start new thread
06-19 17:19:49.706: D/dalvikvm(1237): threadid=11: notify debugger
06-19 17:19:49.706: D/dalvikvm(1237): threadid=11 (SharedPreferencesImpl-load): calling run()
06-19 17:19:49.709: D/dalvikvm(1237): threadid=11: exiting
06-19 17:19:49.709: D/dalvikvm(1237): threadid=11: bye!
06-19 17:19:49.721: D/skia(1237): Flag is not 10
06-19 17:19:49.726: D/skia(1237): Flag is not 10
06-19 17:19:49.736: D/skia(1237): Flag is not 10
06-19 17:19:49.762: D/AndroidRuntime(1237): Shutting down VM
06-19 17:19:49.763: W/dalvikvm(1237): threadid=1: thread exiting with uncaught exception (group=0x40f829a8)
06-19 17:19:49.766: E/AndroidRuntime(1237): FATAL EXCEPTION: main
06-19 17:19:49.766: E/AndroidRuntime(1237): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyt.ilm.ilmsarf/com.nyt.ilm.ilmsarf.MainActivity}: java.lang.NullPointerException
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.os.Looper.loop(Looper.java:153)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.ActivityThread.main(ActivityThread.java:5297)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at dalvik.system.NativeStart.main(Native Method)
06-19 17:19:49.766: E/AndroidRuntime(1237): Caused by: java.lang.NullPointerException
06-19 17:19:49.766: E/AndroidRuntime(1237):     at com.nyt.ilm.ilmsarf.MainActivity.onCreate(MainActivity.java:76)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.Activity.performCreate(Activity.java:5122)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
06-19 17:19:49.766: E/AndroidRuntime(1237):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
06-19 17:19:49.766: E/AndroidRuntime(1237):     ... 11 more
06-19 17:19:49.777: D/dalvikvm(1237): GC_CONCURRENT freed 287K, 4% free 9187K/9500K, paused 1ms+2ms, total 20ms

请帮忙。

提前致谢。

1 个答案:

答案 0 :(得分:1)

NullPointerExcception

它导致因为使用null对象或没有定义对象的值。

您使用的是 activity_main.xml ,而您的xml是 adview.xml

因此无法找到listviewadview

试试这个

更改此

 setContentView(R.layout.activity_main);

 setContentView(R.layout.adview.xml);