0

嗨,我想在我正在开发的游戏中构建 Google+ 游戏 API。但是我有一个问题,我不能直接向 API 发送分数,因为游戏没有在 Activity 中运行。所以我想将分数发送到外部类中的函数,所以我尝试了这段代码:

private void updateGameOver(List<TouchEvent> touchEvents) {
        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);          

                    Leaboarder lea = new Leaboarder();
                    lea.sendScore(Counter.getScore());

                    nullify();
                    game.setScreen(new MainMenuScreen(game));
                    return;
            }
        }

    }

但我收到以下错误:

06-27 21:58:30.746: W/dalvikvm(5701): threadid=13: thread exiting with uncaught exception (group=0x40b88300)
06-27 21:58:30.756: E/AndroidRuntime(5701): FATAL EXCEPTION: Thread-46367
06-27 21:58:30.756: E/AndroidRuntime(5701): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-27 21:58:30.756: E/AndroidRuntime(5701):     at android.os.Handler.<init>(Handler.java:121)
06-27 21:58:30.756: E/AndroidRuntime(5701):     at android.app.Activity.<init>(Activity.java:749)
06-27 21:58:30.756: E/AndroidRuntime(5701):     at de.niklaskellner.flydroid.gui.GoogleGame.Leaboarder.<init>(Leaboarder.java:20)
06-27 21:58:30.756: E/AndroidRuntime(5701):     at de.niklaskellner.flydroid.GameScreen.updateGameOver(GameScreen.java:174)
06-27 21:58:30.756: E/AndroidRuntime(5701):     at de.niklaskellner.flydroid.GameScreen.update(GameScreen.java:80)
06-27 21:58:30.756: E/AndroidRuntime(5701):     at de.niklaskellner.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:47)
06-27 21:58:30.756: E/AndroidRuntime(5701):     at java.lang.Thread.run(Thread.java:856)

我的Leaboarder类:

public class Leaboarder extends Activity implements GameHelper.GameHelperListener {

    protected GameHelper mHelper;
    protected int mRequestedClients = GameHelper.CLIENT_GAMES;
    public static int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int score = getIntent().getIntExtra("score", 0);
        sendScore(score);
    }

    public void sendScore(int score) {

                        mHelper = new GameHelper(Leaboarder.this);
                        mHelper.setup(Leaboarder.this, mRequestedClients);

                        GamesClient mGamesClient = mHelper.getGamesClient();
                        if (mHelper.isSignedIn()) {         

                            mGamesClient.submitScore("", score);
                        }
                        else {
                            Toast.makeText(getBaseContext(), "Not Loggin - Score saved offline.", Toast.LENGTH_LONG).show();
                            SharedPreferences pref = getSharedPreferences("offlineScore", 0);
                            int scoreOLD = pref.getInt("topscore", 0);
                            if (score > scoreOLD){
                                pref.edit().putInt("topscore", score).apply();
                            }
                        }




    }
}

我的问题是如何解决这个问题?

4

1 回答 1

0

Just call Looper.prepare(); as second statement in your onCreate method.

于 2013-06-27T21:20:18.337 回答