3

我正在尝试在我的游戏中显示横幅广告,因此我使用 revmob 来显示横幅广告和全屏广告。现在全屏广告正在显示,但横幅广告未显示在 view.addView(banner) 中出现空指针异常。

enter code herepublic class MainActivity extends Activity {
private CCGLSurfaceView mGLSurfaceView;
private boolean isCreated = false;
public static FrameLayout m_rootLayout;

public static String APPLICATION_ID = "514c7c57cee0500d00000001";
public static RevMob revmob;

// This is used to display Toast messages and is not necessary for your app
@Override
protected void onCreate(Bundle savedInstanceState) {
    if (!isCreated) {
        isCreated = true;
    } else {
        return;
    }

    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    revmob = RevMob.start(this, APPLICATION_ID);
    displayRevMob();


    mGLSurfaceView = new CCGLSurfaceView(this);
    setContentView(mGLSurfaceView);


    CCDirector.sharedDirector().attachInView(mGLSurfaceView);

    getScaledCoordinate();

    Global.assetManager = getAssets();
    Global.context = this;
    Global.loadUserInfo();
    CCScene scene = CCScene.node();
    scene.addChild(new SplashScene(), -1);

    CCDirector.sharedDirector().runWithScene(scene);




    //-------------IAP-----------------------
    Log.d(TAG1, "Creating IAB helper.");
        mHelper = new IabHelper(this, base64EncodedPublicKey);
        mHelper.enableDebugLogging(true);
        Log.d(TAG1, "Starting setup.");
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                Log.d(TAG, "Setup finished.");

                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    complain("Problem setting up in-app billing: " + result);
                    return;
                }

                // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
                Log.d(TAG, "Setup successful. Querying inventory.");
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        });


      RevMobBanner banner = revmob.createBanner(this);
       ViewGroup view = (ViewGroup) findViewById(R.id.banner);
       view.addView(banner);


}



public void displayRevMob(){
    revmob.showFullscreen(this);
   }


}

日志猫:

    04-20 14:05:53.591: E/AndroidRuntime(1066): FATAL EXCEPTION: main
04-20 14:05:53.591: E/AndroidRuntime(1066): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.game.puzzlegame/com.game.puzzlegame.MainActivity}: java.lang.NullPointerException
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.os.Looper.loop(Looper.java:137)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at java.lang.reflect.Method.invoke(Method.java:511)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at dalvik.system.NativeStart.main(Native Method)
04-20 14:05:53.591: E/AndroidRuntime(1066): Caused by: java.lang.NullPointerException
04-20 14:05:53.591: E/AndroidRuntime(1066):     at com.game.puzzlegame.MainActivity.onCreate(MainActivity.java:122)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.Activity.performCreate(Activity.java:4465)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-20 14:05:53.591: E/AndroidRuntime(1066):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-20 14:05:53.591: E/AndroidRuntime(1066):     ... 11 more

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <LinearLayout android:id="@+id/banner"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >
 </LinearLayout>

</LinearLayout>
4

1 回答 1

2

问题出在这里:

ViewGroup view = (ViewGroup) findViewById(R.id.banner);
view.addView(banner);

显然findViewById()返回null,这意味着在您的 xml 布局文件中找不到View调用。banner

您已R.layout.activity_main设置为您的内容视图。确保您在此文件中Viewbannerid。

于 2013-04-20T08:43:52.517 回答