gpt4 book ai didi

java - 类无法转换为 android.view.View$OnClickListener

转载 作者:行者123 更新时间:2023-12-01 11:48:16 25 4
gpt4 key购买 nike

我在尝试使用 G+ 登录时遇到一些问题。我认为 G+ 按钮不起作用。

我也尝试过清理该项目。

IDE:Android Studio 1.1.0播放服务:6.5.87

谢谢。

--

登录 Activity :

 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)

最佳答案

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

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

因此,在 onCreate 函数的第 25 行,您尝试将 Activity (LoginActivity) 转换为 android.view.View$OnClickListener。您不能像这样强制转换此类,因为它们不相关。

您需要在 LoginActivity 类的定义中添加 implements OnClickListener 才能使转换正常工作。

现在您只需传递当前的 Activity 即可注册到监听器:

SignInButton.setOnClickListener(this);

关于java - 类无法转换为 android.view.View$OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com