无法将类别转换为android.view.View $ OnClickListener [英] Class cannot be cast to android.view.View$OnClickListener

查看:56
本文介绍了无法将类别转换为android.view.View $ OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试进行G +登录时遇到一些问题.我认为G +按钮不起作用.

I've some problems trying to do G+ Signin. I think the G+ button isn't working.

我也尝试过清理项目.

IDE:Android Studio 1.1.0播放服务:6.5.87

IDE: Android Studio 1.1.0 Play Services: 6.5.87

谢谢.

-

LoginActivity:

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)

推荐答案

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

If you read the Logcat you can see that you have a problem here :

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

因此,在 onCreate 函数的第25行,您尝试将活动( LoginActivity )投射到 android.view.View $ OnClickListener .您不能像这样转换此类,因为它们之间没有关联.

So at line 25 in onCreate function you are trying to cast your activity (LoginActivity) to android.view.View$OnClickListener. You can't cast this class like this since there are not related.

您需要在 LoginActivity 类的定义上添加 implements OnClickListener ,以使转换工作正常.

You need to add implements OnClickListener on the definition of your LoginActivity class to make your cast work.

现在您将能够通过当前的 Activity 来注册侦听器:

And now you will be able to just pass your current Activity to register to the listener:

SignInButton.setOnClickListener(this);

这篇关于无法将类别转换为android.view.View $ OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆