带有StartApp的libGDX

时间:2014-01-04 22:12:05

标签: java android libgdx advertising

我正在使用libGDX框架,当我尝试将类传递给初始化语句的应用程序监听器时,我收到此错误:

    01-04 15:43:13.046: D/SensorManager(19487): onAccuracyChanged :: accuracy = 3
01-04 15:43:38.032: D/ActivityThread(2757): setTargetHeapUtilization:0.25
01-04 15:43:38.032: D/ActivityThread(2757): setTargetHeapIdealFree:8388608
01-04 15:43:38.032: D/ActivityThread(2757): setTargetHeapConcurrentStart:2097152
01-04 15:43:38.373: W/dalvikvm(2757): threadid=1: thread exiting with uncaught exception (group=0x4159a438)
01-04 15:43:38.383: E/AndroidRuntime(2757): FATAL EXCEPTION: main
01-04 15:43:38.383: E/AndroidRuntime(2757): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.audiack.freetappgame/com.audiack.freetappgame.MainActivity}: java.lang.NullPointerException
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.os.Looper.loop(Looper.java:137)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.ActivityThread.main(ActivityThread.java:4950)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at java.lang.reflect.Method.invokeNative(Native Method)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at java.lang.reflect.Method.invoke(Method.java:511)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at dalvik.system.NativeStart.main(Native Method)
01-04 15:43:38.383: E/AndroidRuntime(2757): Caused by: java.lang.NullPointerException
01-04 15:43:38.383: E/AndroidRuntime(2757):     at com.audiack.freetappgame.StartAppHandler.showAds(StartAppHandler.java:13)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at Screens.FreeTappGame.<init>(FreeTappGame.java:25)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at com.audiack.freetappgame.MainActivity.onCreate(MainActivity.java:24)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.Activity.performCreate(Activity.java:5179)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-04 15:43:38.383: E/AndroidRuntime(2757):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
01-04 15:43:38.383: E/AndroidRuntime(2757):     ... 11 more

我正在尝试将带有接口的类传递给ApplicationListener的构造函数,因此我可以从libGDX通用框架访问android广告方法。

这是活动类:

package com.audiack.freetappgame;

import Screens.FreeTappGame;
import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.badlogic.gdx.graphics.Texture;
import com.searchboxsdk.android.StartAppSearch;
import com.startapp.android.publish.StartAppAd;

public class MainActivity extends AndroidApplication{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();

        StartAppAd.init(this, "101164873", "201640058");
        StartAppSearch.init(this, "101164873", "201640058");

        Texture.setEnforcePotImages(false);
        cfg.useGL20 = true;
        initialize(new FreeTappGame(new StartAppHandler()), cfg);
    }
}

这是我想在android端实现的接口:

package UtilityClasses;

public interface StartAppResolver {
    public boolean showAds(boolean show);
}

这是在android项目中实现接口的类:

package com.audiack.freetappgame;

import UtilityClasses.StartAppResolver;

import com.badlogic.gdx.Gdx;
import com.startapp.android.publish.StartAppAd;

public class StartAppHandler implements StartAppResolver{
protected StartAppAd startAppAd;

@Override
public boolean showAds(boolean show) {
    Gdx.app.log("Ad", "ad command will be here");
    return true;
}
}

最后是ApplicationListener类:

package Screens;


import UtilityClasses.Assets;
import UtilityClasses.StartAppResolver;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;

public class FreeTappGame extends Game implements ApplicationListener {
    static float Width;
    static float Height;

    public StartAppResolver startAppResolver;

    public SplashScreen splash_screen;
    public MenuScreen menu_screen;
    public GameScreen game_screen;
    public HighscoreScreen highscore_screen;
    public AboutScreen about_screen;

    public FreeTappGame(StartAppResolver startAppResolver) {
        this.startAppResolver = startAppResolver;
        startAppResolver.showAds(true);
    }

    @Override
    public void create() {
        Width = Gdx.graphics.getWidth();
        Height = Gdx.graphics.getHeight(); 
        Assets.load();

        splash_screen = new SplashScreen(this);
        menu_screen = new MenuScreen(this);
        game_screen = new GameScreen(this);
        highscore_screen = new HighscoreScreen(this);
        about_screen = new AboutScreen(this);

        setScreen(splash_screen);
    }

}

我是以错误的方式来做这件事的吗?我不确定如何使用libGDX访问广告方法,这是我的最终目标。

1 个答案:

答案 0 :(得分:1)

我认为这一行导致了NullPointerException

Gdx.app.log("Ad", "ad command will be here");

那是因为你在GDX库初始化之前创建了FreeTappGame对象,所以当调用这个方法时,Libgdx还没有初始化Gdx.app全局。