类不能转换为android.view.View $ OnClickListener

时间:2015-03-11 03:16:13

标签: java android

我尝试做G + Signin时遇到了一些问题。我认为G +按钮不起作用。

我也试图清理这个项目。

IDE:Android Studio 1.1.0 游戏服务:6.5.87

THX。

-

LoginActivity:

 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.
    }
}

logcat的:

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)

1 个答案:

答案 0 :(得分:1)

如果你阅读了Logcat,你会发现这里有问题:

com.nimbus.podchest.LoginActivity.onCreate(LoginActivity.java:25)

因此,在onCreate函数的第25行,您正尝试将您的活动(LoginActivity)投射到android.view.View$OnClickListener。你不能像这样投这个类,因为没有相关的。

您需要在implements OnClickListener课程的定义中添加LoginActivity才能使您的演员工作。

现在你可以通过当前的Activity注册给听众:

SignInButton.setOnClickListener(this);