我不知道我做错了什么,但我的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个小时。希望有人能帮帮我!
答案 0 :(得分:3)
如果在布局文件中将Button替换为SignInButton,请尝试刷新和清除项目。还要检查布局文件中是否有另一个Button,其中包含@ + id / sign_in_button
答案 1 :(得分:1)
您正试图将sign_in_button
置于您的布局activity_login
下,而不是等同于您在activity_login
中描述的类或超类。
根据您的布局sign_in_button
,Button
为activity_login
。所以试着用同一个类或它的超类来演员。
以下是您ClassCastException
的根本原因的行代码。
mSignInButton = (Button) findViewById(R.id.sign_in_button);
您也可以尝试投放Button
View
的超级类
mSignInButton = (View) findViewById(R.id.sign_in_button);
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);
答案 2 :(得分:0)
而不是私有SignInButton signInButton尝试给出私有View signInButton.I猜你的自定义类SignInButton你可能有扩展View而不是Button。
答案 3 :(得分:0)
如果您使用的是SignInButton google类,请添加 SignInButton signInButton =(SignInButton)findViewById(R.id.googleSignIn);
确保你会投射按钮
我今天这样做了,它解决了我的问题。