2

何が間違っているのかわかりませんが、Google+ の SignInButton で次のエラーが発生し続けます

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

1

sign_in_buttonレイアウトの下で、activity_loginそのクラスまたはそのスーパークラスと同等ではないクラスにキャストしようとしています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.I を与えてみてください。customclass SignInButton では、Button の代わりに View を拡張した可能性があります。

于 2013-09-02T17:51:45.253 に答える