2

我不知道我做错了什么,但我的 SignInButton for Google+ 不断出现以下错误

09-02 19:26:56.245  12537-12537/com.ferdiseegers.klantenbestand E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ferdiseegers.klantenbestand/com.ferdiseegers.klantenbestand.Activities.LoginActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to com.google.android.gms.common.SignInButton
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
        at android.app.ActivityThread.access$600(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5289)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to com.google.android.gms.common.SignInButton
        at com.ferdiseegers.klantenbestand.Activities.LoginActivity.onCreate(LoginActivity.java:81)
        at android.app.Activity.performCreate(Activity.java:5133)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
        ... 11 more

我在示例项目的布局 XML 中使用了按钮

<com.google.android.gms.common.SignInButton
     android:id="@+id/sign_in_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginBottom="30dip"/>

这就是我在活动中使用它的方式

    public class LoginActivity extends Activity implements View.OnClickListener,
            PlusClient.ConnectionCallbacks, PlusClient.OnConnectionFailedListener,
            PlusClient.OnAccessRevokedListener {

        private static final int DIALOG_GET_GOOGLE_PLAY_SERVICES = 1;

        private static final int REQUEST_CODE_SIGN_IN = 1;
        private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 2;

        private TextView mSignInStatus;
        private PlusClient mPlusClient;
        private SignInButton mSignInButton;
        private View mSignOutButton;
        private View mRevokeAccessButton;
        private ConnectionResult mConnectionResult;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            mPlusClient = new PlusClient.Builder(this, this, this)
                    .setVisibleActivities("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
                    .setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE)
                    .build();

            mSignInStatus = (TextView) findViewById(R.id.sign_in_status);
            mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
            mSignInButton.setOnClickListener(this);
            mSignOutButton = findViewById(R.id.sign_out_button);
            mSignOutButton.setOnClickListener(this);
            mRevokeAccessButton = findViewById(R.id.revoke_access_button);
            mRevokeAccessButton.setOnClickListener(this);
        }

    //some other methods not worth pasting here

}

它可能很简单,但我已经为此苦苦挣扎了 2 个小时。希望任何人都可以帮我一把!

4

4 回答 4

3

如果您在布局文件中将 Button 替换为 SignInButton,请尝试刷新并清除项目。还要检查您在布局文件中是否有另一个按钮 @+id/sign_in_button

于 2013-09-02T18:48:20.270 回答
1

您正在尝试将您sign_in_button的布局activity_login转换为 Class ,该 Class 不等同于您在 that 中描述的类或超类activity_login

根据假设你sign_in_buttonButton你的布局之下activity_login。所以尝试使用相同的类或它的超类进行投射。

这是行的代码,它是您的ClassCastException.

mSignInButton = (Button) findViewById(R.id.sign_in_button);

您也可以尝试将其转换为超ButtonView

mSignInButton = (View) findViewById(R.id.sign_in_button);
  1. 笔记: Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

例如,以下代码生成 ClassCastException:

 Object x = new Integer(0);
 System.out.println((String)x);
于 2013-09-02T17:51:42.597 回答
0

而不是私有的 SignInButton signInButton 尝试提供私有的 View signInButton。我猜对于您的自定义类 SignInButton,您可能扩展了 View 而不是 Button。

于 2013-09-02T17:51:45.253 回答
0

如果您使用 SignInButton google 类,则添加 SignInButton signInButton = (SignInButton) findViewById(R.id.googleSignIn);

确保你会施放按钮

我今天做了这个,它解决了我的问题。

于 2018-04-05T01:33:49.813 回答