我在创建谷歌 api 客户端连接首选项屏幕时遇到了问题。谷歌 api 客户端在 oncreateView(( 方法中连接成功,但我的问题是谷歌 api 客户端必须在谷歌 API 上的首选项开关时连接,谷歌 API 应该连接。
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
当它放在oncreateView((中时,上面工作正常。
record.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValues) {
if (((Boolean) newValues)) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Toast.makeText(getActivity(), "Connection On", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Connection OFF", Toast.LENGTH_SHORT).show();
}
return true;
}
});
在上面的代码中,谷歌api客户端连接被放置在首选项开关内,不工作,异常正在发生。请帮助我如何解决这个问题。
单击首选项开关时显示此异常
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
06-30 12:45:59.810 4029-4029/com.seyali.callLog E/MessageQueue-JNI: java.lang.ClassCastException: com.seyali.callLog.SettingsActivity cannot be cast to com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
at com.seyali.callLog.PreferenceFragments$4.onPreferenceClick(PreferenceFragments.java:233)
at android.preference.Preference.performClick(Preference.java:996)
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:249)
在这样的活动中实现GoogleApiClient.ConnectionCallbacks和GoogleApiClient.OnConnectionFailedListener
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {}