谷歌+安卓版(出现空指针)

31

我正在尝试通过这份官方Google文档将Google+整合到Android中。按照给出的步骤:

  1. 下载最新的SDK。
  2. 使用Android 4.4.2。
  3. 配置Eclipse来使用Java 1.7(尽管它说要使用1.6,但我猜这不是我的问题)。
  4. 然后通过提供Package.NameSha1启用了Google+ API。之后,我将导入的google-play-services_lib附加到示例应用程序中进行配置。

现在运行应用程序时,在MainActivity中出现异常。

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(Plus.API, null) // here the exception (nullPointerException) 
    .addScope(Plus.SCOPE_PLUS_LOGIN).build();

我正在物理设备Android 4.1.2上运行它。

通过Google搜索,但没有找到任何结果。

LogCat:

05-27 10:23:46.808: D/ActivityThread(11136): setTargetHeapUtilization:0.25 05-27 10:23:46.808: D/ActivityThread(11136): setTargetHeapIdealFree:8388608 05-27 10:23:46.808: D/ActivityThread(11136): setTargetHeapConcurrentStart:2097152 05-27 10:23:47.228: D/AndroidRuntime(11136): Shutting down VM 05-27 10:23:47.228: W/dalvikvm(11136): threadid=1: thread exiting with uncaught exception (group=0x411eb438) 05-27 10:23:47.228: E/AndroidRuntime(11136): FATAL EXCEPTION: main 05-27 10:23:47.228: E/AndroidRuntime(11136): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mygoogleplus/com.example.mygoogleplus.GooglePlusMainActivity}: java.lang.NullPointerException: Null options are not permitted for this Api 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.ActivityThread.access$700(ActivityThread.java:143) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.os.Handler.dispatchMessage(Handler.java:99) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.os.Looper.loop(Looper.java:137) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.ActivityThread.main(ActivityThread.java:4960) 05-27 10:23:47.228: E/AndroidRuntime(11136): at java.lang.reflect.Method.invokeNative(Native Method) 05-27 10:23:47.228: E/AndroidRuntime(11136): at java.lang.reflect.Method.invoke(Method.java:511) 05-27 10:23:47.228: E/AndroidRuntime(11136): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 05-27 10:23:47.228: E/AndroidRuntime(11136): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 05-27 10:23:47.228: E/AndroidRuntime(11136): at dalvik.system.NativeStart.main(Native Method) 05-27 10:23:47.228: E/AndroidRuntime(11136): Caused by: java.lang.NullPointerException: Null options are not permitted for this Api 05-27 10:23:47.228: E/AndroidRuntime(11136): at com.google.android.gms.internal.fq.b(Unknown Source) 05-27 10:23:47.228: E/AndroidRuntime(11136): at com.google.android.gms.common.api.GoogleApiClient$Builder.addApi(Unknown Source) 05-27 10:23:47.228: E/AndroidRuntime(11136): at com.example.mygoogleplus.GooglePlusMainActivity.onCreate(GooglePlusMainActivity.java:89) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.Activity.performCreate(Activity.java:5203) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 05-27 10:23:47.228: E/AndroidRuntime(11136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)

3个回答

119

以下方法对我有效。

只需以这种方式传递一个参数。

.addApi(Plus.API) 

取代

.addApi(Plus.API, null)

或者

你也可以传递一个 PlusOptions 对象

.addApi(Plus.API, Plus.PlusOptions.builder().build())

这两种解决方案都可以。


这个也解决了我的问题。我必须编辑GameHelper.java文件:if (0 != (mRequestedClients & CLIENT_PLUS)) { builder.addApi(Plus.API);/*, mPlusApiOptions);*/ builder.addScope(Plus.SCOPE_PLUS_LOGIN); } - runfaj
6
这个 G+ 的文档里充斥着一些愚蠢的问题,感谢你为他们背锅!("covering for them" 在此指“替他们承担责任”) - CodingIntrigue
你很棒,我非常感激你。我被这个问题困扰了4个月,而且不知道实际问题是什么。非常感谢你。 - Usman

3
mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN) 
                .build();

这对我很有用。mGoogleApiClient是GoogleApiClient对象。只需将一个参数传递给addApi()函数即可。例如:

.addApi(Plus.API)

3
你应该为该API构建选项。
.addApi(Plus.API, PlusOptions.builder().build())

如果您使用GameHelper,您应该在设置之前调用setPlusApiOptions。
// Google Play Services
m_GameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES | GameHelper.CLIENT_PLUS);
m_GameHelper.setPlusApiOptions( PlusOptions.builder().build() );
m_GameHelper.setup(this);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接