实例化GameHelper libgdx

时间:2014-03-09 22:24:45

标签: android libgdx

我一直在google和其他使用LibGdx实现GameHelper的网站上的教程,我不知道为什么会出现这个错误

这是我的主要活动

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.CrazyEagle.utils.AdsHandler;
import com.CrazyEagle.utils.GameAction;
import com.badlogic.gdx.backends.android.AndroidApplication;
 import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
 import com.google.android.gms.ads.AdView;

import com.google.example.games.basegameutils.GameHelper;



   public class MainActivity extends AndroidApplication implements AdsHandler, GameAction, GameHelper.GameHelperListener{
static AdView adView;
static RelativeLayout.LayoutParams adParams;
GameHelper mHelper;



private static final String AD_UNIT_ID = "cid";


private final static int SHOW_ADS = 1;
private final static int HIDE_ADS = 0;
private final static int TOP = 1;
private final static int BOTTOM = 0;

final static int RC_UNUSED = 9002;


public MainActivity(){

    mHelper = new GameHelper(this, GameHelper.CLIENT_ALL);

}




@Override
protected void onActivityResult(int requestCode, int response, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, response, data);
    mHelper.onActivityResult(requestCode, response, data);
}




protected static Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
            case SHOW_ADS:
            {
                adView.setVisibility(View.VISIBLE);
                break;
            }
            case HIDE_ADS:
            {
                adView.setVisibility(View.GONE);
                break;
            }
        }

        switch(msg.what){
        case TOP:
            adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            adParams.addRule(RelativeLayout.ALIGN_TOP);
            break;
        case BOTTOM:
            adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            adParams.addRule(RelativeLayout.ALIGN_BOTTOM);
            break;
        }
    }
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mHelper.setup(this);

    RelativeLayout layout = new RelativeLayout(this);


    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    View gameView  = initializeForView(new GameActivity(this));




      AdRequest adRequest = new AdRequest.Builder()

        .addTestDevice("")
        .build();
    adView = new AdView(this);
    adView.setAdSize(AdSize.FULL_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);
    adView.loadAd(adRequest);


    RelativeLayout.LayoutParams gameParams = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
      gameParams.addRule(RelativeLayout.CENTER_IN_PARENT);
      gameParams.bottomMargin = 1;

   adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);




    layout.addView(gameView, gameParams);
    layout.addView(adView, adParams);

    setContentView(layout);
}


@Override
protected void onDestroy() {
     if (adView != null) {
          adView.destroy();
        }

    super.onDestroy();
}



@Override
protected void onPause() {
     if (adView != null) {
          adView.pause();
        }

    super.onPause();
}



@Override
protected void onResume() {
    if (adView != null) {
          adView.resume();
        }
    super.onResume();
}


@Override
public void showAds(boolean show) {
    handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);

}


@Override
public void setLocation(boolean loc) {
    handler.sendEmptyMessage(loc ? TOP : BOTTOM);

}


@Override
public void Login() {
    try {
        runOnUiThread(new Runnable(){

        //@Override
        public void run(){
        mHelper.beginUserInitiatedSignIn();
        }
        });
        }catch (final Exception ex){

        }

}


@Override
public void LogOut() {
    try {
        runOnUiThread(new Runnable(){

        //@Override
        public void run(){
        mHelper.signOut();
        }
        });
        }catch (final Exception ex){

        }

}


@Override
public boolean getSignedIn() {
    return mHelper.isSignedIn();
}


@Override
public void onSignInFailed() {
    System.out.println("sign in failed");

}


@Override
public void onSignInSucceeded() {
    System.out.println("sign in succeeded");

}

  }

这是我的日志

                03-09 15:17:15.813: E/AndroidRuntime(25395): FATAL EXCEPTION: main
                03-09 15:17:15.813: E/AndroidRuntime(25395): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.CrazyEagle/com.CrazyEagle.MainActivity}: java.lang.NullPointerException
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.os.Handler.dispatchMessage(Handler.java:99)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.os.Looper.loop(Looper.java:137)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.app.ActivityThread.main(ActivityThread.java:5419)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at java.lang.reflect.Method.invokeNative(Native Method)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at java.lang.reflect.Method.invoke(Method.java:525)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at dalvik.system.NativeStart.main(Native Method)
                03-09 15:17:15.813: E/AndroidRuntime(25395): Caused by: java.lang.NullPointerException
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at com.google.example.games.basegameutils.GameHelper.<init>(GameHelper.java:172)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at com.CrazyEagle.MainActivity.<init>(MainActivity.java:44)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at java.lang.Class.newInstanceImpl(Native Method)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at java.lang.Class.newInstance(Class.java:1130)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
                03-09 15:17:15.813: E/AndroidRuntime(25395):    ... 11 more

任何帮助都将被appricaited

2 个答案:

答案 0 :(得分:0)

放置mHelper = new GameHelper(this,GameHelper.CLIENT_ALL);在错误的地方。它应该在onCreate()

中启动

答案 1 :(得分:0)

尝试put requestWindowFeature(Window.FEATURE_NO_TITLE);在super.onCreate之前(savedInstanceState);