2016-08-16 69 views
0

我試圖效仿谷歌的說明導入谷歌Play遊戲服務在: https://developers.google.com/games/services/training/signin谷歌按鈕「不能被轉換爲android.widget.Button」

<!-- sign-in button --> 
<com.google.android.gms.common.SignInButton 
android:id="@+id/sign_in_button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 

當我導入谷歌官方的播放按鈕(作爲XML佈局如上圖所示),我得到這個錯誤在我的日誌:

java.lang.RuntimeException: Unable to start activity ComponentInfo{flash4life.gespant2015.com.flashreflex20/flash4life.gespant2015.com.flashreflex20.MainActivity}: java.lang.ClassCastException: com.google.android.gms.common.SignInButton cannot be cast to android.widget.Button 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3255) 

這是整個錯誤日誌:

08-15 19:57:25.961 472-472/flash4life.gespant2015.com.flashreflex20 E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: flash4life.gespant2015.com.flashreflex20, PID: 472 
                        java.lang.RuntimeException: Unable to start activity ComponentInfo{flash4life.gespant2015.com.flashreflex20/flash4life.gespant2015.com.flashreflex20.MainActivity}: java.lang.ClassCastException: com.google.android.gms.common.SignInButton cannot be cast to android.widget.Button 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3255) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351) 
                         at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796) 
                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                         at android.os.Looper.loop(Looper.java:158) 
                         at android.app.ActivityThread.main(ActivityThread.java:7230) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                        Caused by: java.lang.ClassCastException: com.google.android.gms.common.SignInButton cannot be cast to android.widget.Button 
                         at flash4life.gespant2015.com.flashreflex20.MainActivity.onCreate(MainActivity.java:107) 
                         at android.app.Activity.performCreate(Activity.java:6877) 
                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) 
                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3208) 
                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351)  
                         at android.app.ActivityThread.access$1100(ActivityThread.java:222)  
                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796)  
                         at android.os.Handler.dispatchMessage(Handler.java:102)  
                         at android.os.Looper.loop(Looper.java:158)  
                         at android.app.ActivityThread.main(ActivityThread.java:7230)  
                         at java.lang.reflect.Method.invoke(Native Method)  
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  

我分配的按鈕在我的Java:

signInButton = (Button) findViewById(R.id.sign-in-button); 

回答

0

雖然它有它的字按鈕,按照SignInButton documentationSignInButton實際上是擴展了FrameLayout

每個View都支持設置一個OnClickListener,所以根本沒有理由將其轉換爲Button

0

這是SignInButton文檔,它是一個FrameLayout裏沒有一個按鈕 公共final類SignInButton擴展FrameLayout實現View.OnClickListener

但它有你可以使用的功能 void setOnClickListener(View.OnClickListener聽衆)

所以你可以這樣做 signInButton =(SignInButton)findViewById(R.id.sign-in-button); signInButton.setOnClickListener(new OnCLickListener)

相關問題