在Android上实现google play服务登录

时间:2014-07-16 14:48:30

标签: android eclipse google-play-services

我正在尝试按照本指南https://developers.google.com/games/services/training/signin实施Google Play服务登录但是当活动开始时,我收到此错误:

07-16 16:37:34.813: E/AndroidRuntime(7732): FATAL EXCEPTION: main 07-16 16:37:34.813: E/AndroidRuntime(7732): java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information. 07-16 16:37:34.813: E/AndroidRuntime(7732): at com.google.android.gms.internal.hc$h.b(Unknown Source) 07-16 16:37:34.813: E/AndroidRuntime(7732): at com.google.android.gms.internal.hc$h.d(Unknown Source) 07-16 16:37:34.813: E/AndroidRuntime(7732): at com.google.android.gms.internal.hc$b.fq(Unknown Source) 07-16 16:37:34.813: E/AndroidRuntime(7732): at com.google.android.gms.internal.hc$a.handleMessage(Unknown Source) 07-16 16:37:34.813: E/AndroidRuntime(7732): at android.os.Handler.dispatchMessage(Handler.java:99) 07-16 16:37:34.813: E/AndroidRuntime(7732): at android.os.Looper.loop(Looper.java:137) 07-16 16:37:34.813: E/AndroidRuntime(7732): at android.app.ActivityThread.main(ActivityThread.java:5306) 07-16 16:37:34.813: E/AndroidRuntime(7732): at java.lang.reflect.Method.invokeNative(Native Method) 07-16 16:37:34.813: E/AndroidRuntime(7732): at java.lang.reflect.Method.invoke(Method.java:511) 07-16 16:37:34.813: E/AndroidRuntime(7732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 07-16 16:37:34.813: E/AndroidRuntime(7732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 07-16 16:37:34.813: E/AndroidRuntime(7732): at dalvik.system.NativeStart.main(Native Method)

这是我正在尝试开始的活动的完整代码:

public class Test extends BaseGameActivity实现OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    findViewById(R.id.sign_in_button).setOnClickListener(this);
    findViewById(R.id.sign_out_button).setOnClickListener(this);
}

@Override
public void onSignInFailed() {
    // TODO Auto-generated method stub
    findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
    findViewById(R.id.sign_out_button).setVisibility(View.GONE);
}

@Override
public void onSignInSucceeded() {
    // TODO Auto-generated method stub
    // show sign-out button, hide the sign-in button
    findViewById(R.id.sign_in_button).setVisibility(View.GONE);
    findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
}
@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    if (view.getId() == R.id.sign_in_button) {
        // start the asynchronous sign in flow
        beginUserInitiatedSignIn();
    }
    else if (view.getId() == R.id.sign_out_button) {
        // sign out.
        signOut();

        // show sign-in button, hide the sign-out button
        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.sign_out_button).setVisibility(View.GONE);
    }
}
public abstract class BaseGameActivity extends FragmentActivity implements
    GameHelper.GameHelperListener {

    // The game helper object. This class is mainly a wrapper around this object.
    protected GameHelper mHelper;

    // We expose these constants here because we don't want users of this class
    // to have to know about GameHelper at all.
    public static final int CLIENT_GAMES = GameHelper.CLIENT_GAMES;
    public static final int CLIENT_APPSTATE = GameHelper.CLIENT_APPSTATE;
    public static final int CLIENT_PLUS = GameHelper.CLIENT_PLUS;
    public static final int CLIENT_ALL = GameHelper.CLIENT_ALL;

    // Requested clients. By default, that's just the games client.
    protected int mRequestedClients = CLIENT_GAMES;

    private final static String TAG = "BaseGameActivity";
    protected boolean mDebugLog = false;

    /** Constructs a BaseGameActivity with default client (GamesClient). */
    protected BaseGameActivity() {
        super();
    }

    /**
     * Constructs a BaseGameActivity with the requested clients.
     * @param requestedClients The requested clients (a combination of CLIENT_GAMES,
     *         CLIENT_PLUS and CLIENT_APPSTATE).
     */
    protected BaseGameActivity(int requestedClients) {
        super();
        setRequestedClients(requestedClients);
    }

    /**
     * Sets the requested clients. The preferred way to set the requested clients is
     * via the constructor, but this method is available if for some reason your code
     * cannot do this in the constructor. This must be called before onCreate or getGameHelper()
     * in order to have any effect. If called after onCreate()/getGameHelper(), this method
     * is a no-op.
     *
     * @param requestedClients A combination of the flags CLIENT_GAMES, CLIENT_PLUS
     *         and CLIENT_APPSTATE, or CLIENT_ALL to request all available clients.
     */
    protected void setRequestedClients(int requestedClients) {
        mRequestedClients = requestedClients;
    }

    public GameHelper getGameHelper() {
        if (mHelper == null) {
            mHelper = new GameHelper(this, mRequestedClients);
            mHelper.enableDebugLog(mDebugLog);
        }
        return mHelper;
    }

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        if (mHelper == null) {
            getGameHelper();
        }
        mHelper.setup(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mHelper.onStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHelper.onStop();
    }

    @Override
    protected void onActivityResult(int request, int response, Intent data) {
        super.onActivityResult(request, response, data);
        mHelper.onActivityResult(request, response, data);
    }

    protected GoogleApiClient getApiClient() {
        return mHelper.getApiClient();
    }

    protected boolean isSignedIn() {
        return mHelper.isSignedIn();
    }

    protected void beginUserInitiatedSignIn() {
        mHelper.beginUserInitiatedSignIn();
    }

    protected void signOut() {
        mHelper.signOut();
    }

    protected void showAlert(String message) {
        mHelper.makeSimpleDialog(message).show();
    }

    protected void showAlert(String title, String message) {
        mHelper.makeSimpleDialog(title, message).show();
    }

    protected void enableDebugLog(boolean enabled) {
        mDebugLog = true;
        if (mHelper != null) {
            mHelper.enableDebugLog(enabled);
        }
    }

    @Deprecated
    protected void enableDebugLog(boolean enabled, String tag) {
        Log.w(TAG, "BaseGameActivity.enabledDebugLog(bool,String) is " +
                "deprecated. Use enableDebugLog(boolean)");
        enableDebugLog(enabled);
    }

    protected String getInvitationId() {
        return mHelper.getInvitationId();
    }

    protected void reconnectClient() {
        mHelper.reconnectClient();
    }

    protected boolean hasSignInError() {
        return mHelper.hasSignInError();
    }

    protected GameHelper.SignInFailureReason getSignInError() {
        return mHelper.getSignInError();
    }


    }

}

3 个答案:

答案 0 :(得分:2)

如果您的包名或app_id不可用,则会抛出错误!

当您想要实施Google Play服务时,您必须在Google Play中创建应用,获取app_id并指定包名称; 如果没问题,那么您将更改您的应用程序的软件包名称是您自己的软件包名称(与您在Developer Console中注册的名称相同)就像com.example。*。 您将修改res / values / ids.xml或其他文件中的app_id;

你可以获得更多帮助 https://github.com/playgameservices/android-samples 部分"修改ID,编译和运行"非常有用。

祝你好运!

答案 1 :(得分:1)

确保您在清单中使用了正确版本的APP_ID。

对于访问GameHelper.CLIENT_APPSTATE的客户,您需要

<meta-data android:name="com.google.android.gms.appstate.APP_ID"
           android:value="@string/app_id" />

对于访问GameHelper.CLIENT_SNAPSHOT的人,您需要

<meta-data android:name="com.google.android.gms.games.APP_ID" 
           android:value="@string/app_id" />

保存游戏快照是一项旨在取代appstate的新功能,但是这种错误条件在从一个迁移到另一个时很容易运行。与说明您应该查看错误日志的异常相反,实际上没有日志条目显示在抛出异常之前的任何错误。

答案 2 :(得分:0)

你不应该使用BaseGameActivity作为内部类。您应该从库中引用它。 Follow this link and follow the steps 1-8 under "Setting up your game project"

编辑:

首先,在项目中引用 BaseGameUtils 库。

现在看看这段代码:

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

public class Test extends BaseGameActivity { //contained in the library
    // override unimplemented methods here.
}

这就是你真正需要的。