我正在尝试在我正在创建的安卓游戏中实现Facebook SDK。
我创建了一个主游戏,我的所有逻辑都发生了,我创建了一个使用主人的Android版本。希望这是有道理的。
无论如何为了触发按钮在我的主代码中按下android版本中的代码我试图使用接口来完成此操作但我不断收到以下错误;
致命的例外:GLThread 56457 显示java.lang.NullPointerException
我不确定我做错了什么,并感谢任何帮助。我把我的代码放在下面。
FacebookInterface.java(主)
package com.gksoftworks.ECHelpers;
public interface FacebookInterface {
public void FacebookShare();
}
InputHandler.java(master)
package com.gksoftworks.ECHelpers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.gksoftworks.GameWorld.GameWorld;
import com.gksoftworks.ui.*;
import com.gksoftworks.GameObjects.Chick;
public class InputHandler implements InputProcessor {
private Chick myChick;
private GameWorld myWorld;
private List<SimpleButton> menuButtons;
private List<SimpleButton> gameOverButtons;
// List of all buttons
private SimpleButton easyButton;
private SimpleButton mediumButton;
private SimpleButton hardButton;
private SimpleButton randomButton;
private SimpleButton rateButton;
private SimpleButton shareButton;
private SimpleButton difficultyButton;
private SimpleButton retryButton;
private float scaleFactorX;
private float scaleFactorY;
private FacebookInterface fbInterface;
public InputHandler(GameWorld myWorld, float scaleFactorX,
float scaleFactorY) {
this.myWorld = myWorld;
myChick = myWorld.getChick();
int midPointY = myWorld.getMidPointY();
this.scaleFactorX = scaleFactorX;
this.scaleFactorY = scaleFactorY;
menuButtons = new ArrayList<SimpleButton>();
gameOverButtons = new ArrayList<SimpleButton>();
// Add the easy button
easyButton = new SimpleButton(
136 / 2 - (AssetLoader.easyButton.getRegionWidth() / 2),
midPointY + -11, 24, 16, AssetLoader.easyButton,
AssetLoader.easyButton);
menuButtons.add(easyButton);
// Add the medium button
mediumButton = new SimpleButton(
136 / 2 - (AssetLoader.mediumButton.getRegionWidth() / 2),
midPointY + 9, 35, 16, AssetLoader.mediumButton,
AssetLoader.mediumButton);
menuButtons.add(mediumButton);
// Add the hard button
hardButton = new SimpleButton(
136 / 2 - (AssetLoader.hardButton.getRegionWidth() / 2),
midPointY + 29, 25, 16, AssetLoader.hardButton,
AssetLoader.hardButton);
menuButtons.add(hardButton);
// Add the random button
randomButton = new SimpleButton(
136 / 2 - (AssetLoader.randomButton.getRegionWidth() / 2),
midPointY + 48, 37, 16, AssetLoader.randomButton,
AssetLoader.randomButton);
menuButtons.add(randomButton);
// Add the rate button
rateButton = new SimpleButton(5, 2, 60, 16, AssetLoader.rateButton, AssetLoader.rateButton);
menuButtons.add(rateButton);
// Add the share button
shareButton = new SimpleButton(70, 2, 60, 16, AssetLoader.shareButton, AssetLoader.shareButton);
menuButtons.add(shareButton);
// Add the rate button
difficultyButton = new SimpleButton(
136 / 2 - (AssetLoader.difficultyButton.getRegionWidth() / 2),
midPointY + 16, 65, 16, AssetLoader.difficultyButton,
AssetLoader.difficultyButton);
gameOverButtons.add(difficultyButton);
// Add the rate button
retryButton = new SimpleButton(
136 / 2 - (AssetLoader.retry.getRegionWidth() / 2),
midPointY + 38, 33, 16, AssetLoader.retry,
AssetLoader.retry);
gameOverButtons.add(retryButton);
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX = scaleX(screenX);
screenY = scaleY(screenY);
if (myWorld.isMenu()) {
easyButton.isTouchDown(screenX, screenY);
mediumButton.isTouchDown(screenX, screenY);
hardButton.isTouchDown(screenX, screenY);
randomButton.isTouchDown(screenX, screenY);
rateButton.isTouchDown(screenX, screenY);
shareButton.isTouchDown(screenX, screenY);
} else if (myWorld.isReady()) {
myWorld.start();
myChick.onClick();
} else if (myWorld.isRunning()) {
myChick.onClick();
}
if (myWorld.isGameOver() || myWorld.isHighScore()) {
difficultyButton.isTouchDown(screenX, screenY);
retryButton.isTouchDown(screenX, screenY);
}
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX = scaleX(screenX);
screenY = scaleY(screenY);
if (myWorld.isMenu()) {
if (easyButton.isTouchUp(screenX, screenY)) {
myWorld.ready(1);
return true;
} else if (mediumButton.isTouchUp(screenX, screenY)) {
myWorld.ready(2);
return true;
} else if (hardButton.isTouchUp(screenX, screenY)) {
myWorld.ready(3);
return true;
} else if (randomButton.isTouchUp(screenX, screenY)) {
myWorld.ready(4);
return true;
} else if (rateButton.isTouchUp(screenX, screenY)) {
Gdx.net.openURI("https://play.google.com/store/apps/details?id=com.gksoftworks.EasterChick");
} else if (shareButton.isTouchUp(screenX, screenY)) {
fbInterface.FacebookShare();
}
} else if (myWorld.isGameOver()) {
if (difficultyButton.isTouchUp(screenX, screenY)) {
myWorld.restart(true);
} else if (retryButton.isTouchUp(screenX, screenY)) {
myWorld.restart(false);
}
}
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
private int scaleX(int screenX) {
return (int) (screenX / scaleFactorX);
}
private int scaleY(int screenY) {
return (int) (screenY / scaleFactorY);
}
public List<SimpleButton> getMenuButtons() {
return menuButtons;
}
public List<SimpleButton> getGameOverButtons() {
return gameOverButtons;
}
@Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub
return false;
}
}
所以我在这里创建了我的Facebook界面,并在按下分享按钮时使用它。
MainActivity.java(Android)
package com.gksoftworks.EasterChick;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.badlogic.gdx.Gdx;
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.gksoftworks.ECHelpers.FacebookInterface;
//import com.google.android.gms.ads.InterstitialAd;
import com.gksoftworks.EasterChick.ECGame;
public class MainActivity extends AndroidApplication implements FacebookInterface {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the layout
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);
// Create the libgdx View
View gameView = initializeForView(new ECGame(), false);
// Create and setup the AdMob view
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-0674036768511172/5754826717");
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequestBanner = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("CB5125BK84")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequestBanner);
// Add the libgdx view
layout.addView(gameView);
// Add the AdMob view
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}
@Override
public void FacebookShare(){
Gdx.app.log("AndroidGame","Hello Android");
}
}
所以我把它作为这个活动的一部分实现,我得到了错误。当我按下按钮时它不会在logcat中显示我在Android中完成的代码吗?
有人可以帮忙吗?
答案 0 :(得分:0)
我如何总是这样做,有点像你,创建一个接口,一些函数通过它调用MainActivity并在核心项目的主游戏类中初始化它。
Game.java
public static FacebookInterface myRequestHandler;
public Game(FacebookInterface handler) {
myRequestHandler = handler;
}
@Override
public void create() {
setScreen(new Splash());
}
@Override
public void dispose() {
super.dispose();
}
etc...
然后我只是在任何类中调用它(不要忘记导入Game类),例如,使用
Game.myRequestHandler.FacebookShare();
放手一搏,希望它能奏效。
答案 1 :(得分:0)
可能存在更大的问题,但看起来InputHandler
中的变量尚未初始化,因此myWorld
为空(以及按钮)。
答案 2 :(得分:0)
FacebookInterface fbInterface
定义不初始化,初始化类必须创建它的实例。因为你有接口,你可能需要创建实现FacebookInterface
接口
您可以使用匿名类
FacebookInterface fbInterface = new FacebookInterface(){
@Override
public void FacebookShare(){
Gdx.app.log("AndroidGame","Hello Android");
}
}
应修复您的NullPointerException