簡體   English   中英

Google+游戲API java.lang.RuntimeException

[英]Google+ Games API java.lang.RuntimeException

嗨,我想在自己開發的游戲中構建Google+游戲API。 但是我有一個問題,因為游戲沒有在Activity中運行,所以我無法直接將分數發送到API。 所以我想將分數發送到外部類中的函數,所以嘗試了以下代碼:

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();
                            }
                        }




    }
}

我的問題是如何解決該問題?

只需調用Looper.prepare(); 作為onCreate方法中的第二條語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM