Java 类不能强制转换为android.view.view$OnClickListener

Java 类不能强制转换为android.view.view$OnClickListener,java,android,Java,Android,我在做G+签名时遇到了一些问题。我想G+按钮坏了 我也试着清理这个项目 IDE:AndroidStudio 1.1.0 游戏服务:6.5.87 Thx -- 后勤活动: package com.nimbus.podchest; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.View; import com.google.android.gms.co

我在做G+签名时遇到了一些问题。我想G+按钮坏了

我也试着清理这个项目

IDE:AndroidStudio 1.1.0 游戏服务:6.5.87

Thx

--

后勤活动:

 package com.nimbus.podchest;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;


public class LoginActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;
    private SignInButton SignInButton;

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

        // Create a GoogleApiClient instance
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        SignInButton = (SignInButton) findViewById(R.id.sign_in_button);

        SignInButton.setOnClickListener((android.view.View.OnClickListener) this);
    }


    public void onClick(View view) {
        if (view.getId() == R.id.sign_in_button) {
            mGoogleApiClient.connect();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
            mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
            mGoogleApiClient.disconnect();
            super.onStop();

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // Connected to Google Play services!
        // The good stuff goes here.
    }

    @Override
    public void onConnectionSuspended(int cause) {
        // The connection has been interrupted.
        // Disable any UI components that depend on Google APIs
        // until onConnected() is called.
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // This callback is important for handling errors that
        // may occur while attempting to connect with Google.
        //
        // More about this in the next section.
    }
}
日志:

03-10 23:58:26.747  10123-10123/com.nimbus.podchest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.nimbus.podchest, PID: 10123
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nimbus.podchest/com.nimbus.podchest.LoginActivity}: java.lang.ClassCastException: com.nimbus.podchest.LoginActivity cannot be cast to android.view.View$OnClickListener
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.ClassCastException: com.nimbus.podchest.LoginActivity cannot be cast to android.view.View$OnClickListener
            at com.nimbus.podchest.LoginActivity.onCreate(LoginActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-11 00:00:13.565  11822-11822/com.nimbus.podchest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.nimbus.podchest, PID: 11822
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nimbus.podchest/com.nimbus.podchest.LoginActivity}: java.lang.ClassCastException: com.nimbus.podchest.LoginActivity cannot be cast to android.view.View$OnClickListener
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.ClassCastException: com.nimbus.podchest.LoginActivity cannot be cast to android.view.View$OnClickListener
            at com.nimbus.podchest.LoginActivity.onCreate(LoginActivity.java:35)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

如果您阅读日志,您会发现您在这里遇到了问题:

com.nimbus.podchest.LoginActivity.onCreate(LoginActivity.java:25)
因此,在
onCreate
函数的第25行,您试图将您的活动(
LoginActivity
)强制转换为
android.view.view$OnClickListener
)。你不能像这样铸造这个类,因为没有相关的

您需要在
LoginActivity
类的定义上添加
implements OnClickListener
,以使您的cast工作

现在,您可以将当前的
活动
传递给侦听器进行注册:

SignInButton.setOnClickListener(this);

LoginActivity需要实现View.OnClickListener接口,因为这一行:
SignInButton.setOnClickListener((android.View.View.OnClickListener)this)如果您有不同的问题,您应该将其作为新问题发布(而不是更改此问题的目的)。我不知道新的错误指示了什么。当然。我要写一篇新文章。非常感谢。