整合google plus时出现运行时错误

时间:2016-01-21 11:54:24

标签: java android

我正在尝试将google plus集成到我的Android应用程序中。但它显示运行时错误。

获取此链接的帮助: https://developers.google.com/+/mobile/android/sign-in

完成LogCat

  01-21 13:08:22.097: W/SignInButton(1171): Sign in button not found, using placeholder instead
  01-21 13:08:22.267: D/AndroidRuntime(1171): Shutting down VM
  01-21 13:08:22.267: W/dalvikvm(1171): threadid=1: thread exiting with uncaught exception (group=0xb3a7cba8)
  01-21 13:08:22.387: D/dalvikvm(1171): GC_FOR_ALLOC freed 100K, 6% free 3189K/3368K, paused 90ms, total 93ms
  01-21 13:08:22.387: E/AndroidRuntime(1171): FATAL EXCEPTION: main
  01-21 13:08:22.387: E/AndroidRuntime(1171): Process: com.example.googleplus, PID: 1171
  01-21 13:08:22.387: E/AndroidRuntime(1171): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googleplus/com.example.googleplus.MainActivity}: java.lang.NullPointerException: Null options are not permitted for this Api
  01-21 13:08:22.387: E/AndroidRuntime(1171):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
  01-21 13:08:22.387: E/AndroidRuntime(1171):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
  01-21 13:08:22.387: E/AndroidRuntime(1171):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
  01-21 13:08:22.387: E/AndroidRuntime(1171):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at   android.os.Handler.dispatchMessage(Handler.java:102)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at android.os.Looper.loop(Looper.java:136)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at android.app.ActivityThread.main(ActivityThread.java:5001)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at java.lang.reflect.Method.invokeNative(Native Method)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at java.lang.reflect.Method.invoke(Method.java:515)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
 01-21 13:08:22.387: E/AndroidRuntime(1171):    at dalvik.system.NativeStart.main(Native Method)
01-21 13:08:22.387: E/AndroidRuntime(1171): Caused by: java.lang.NullPointerException: Null options are not permitted for this Api
01-21 13:08:22.387: E/AndroidRuntime(1171):     at com.google.android.gms.common.internal.o.b(Unknown Source)
01-21 13:08:22.387: E/AndroidRuntime(1171):     at com.google.android.gms.common.api.GoogleApiClient$Builder.addApi(Unknown Source)
01-21 13:08:22.387: E/AndroidRuntime(1171):     at com.example.googleplus.MainActivity.onCreate(MainActivity.java:58)
01-21 13:08:22.387: E/AndroidRuntime(1171):     at android.app.Activity.performCreate(Activity.java:5231)
01-21 13:08:22.387: E/AndroidRuntime(1171):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-21 13:08:22.387: E/AndroidRuntime(1171):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
01-21 13:08:22.387: E/AndroidRuntime(1171):     ... 11 more
01-21 13:08:26.547: I/Process(1171): Sending signal. PID: 1171 SIG: 9

主要活动

 package com.example.googleplus;

 import java.io.IOException;
 import java.net.URI;
 import java.net.URL;

 import org.json.JSONException;
 import org.json.JSONObject;

 import android.app.Activity;
 import android.content.Intent;
 import android.content.IntentSender.SendIntentException;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.os.StrictMode;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;
 import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.api.GoogleApiClient;
 import     com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
 import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
 import com.google.android.gms.plus.Plus;
 import com.google.android.gms.plus.model.people.Person;

 public class MainActivity extends Activity implements ConnectionCallbacks,
          OnConnectionFailedListener {


   private static final int RC_SIGN_IN = 0;


   private GoogleApiClient mGoogleApiClient;

   private boolean mIntentInProgress;
   TextView textView;
   ImageView profileImage;
   com.google.android.gms.common.SignInButton signInBtn;

   public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                       .permitAll().build();
          StrictMode.setThreadPolicy(policy);

          setContentView(R.layout.activity_main);
          mGoogleApiClient = new GoogleApiClient.Builder(this)
                       .addConnectionCallbacks(this)
                       .addOnConnectionFailedListener(this).addApi(Plus.API, null)
                       .addScope(Plus.SCOPE_PLUS_LOGIN).build();

          textView = (TextView) findViewById(R.id.username);
          profileImage = (ImageView) findViewById(R.id.profileImage);
          signInBtn = (com.google.android.gms.common.SignInButton) findViewById(R.id.sign_in_button);
          signInBtn.setOnClickListener(new OnClickListener() {

                 @Override
                 public void onClick(View arg0) {
                       mGoogleApiClient.connect();

                 }
          });
   }

   protected void onStart() {
          super.onStart();

   }

   protected void onStop() {
          super.onStop();

          if (mGoogleApiClient.isConnected()) {
                 mGoogleApiClient.disconnect();
          }
   }

   @Override
   public void onConnectionFailed(ConnectionResult arg0) {

          if (!mIntentInProgress && arg0.hasResolution()) {
                 try {
                       mIntentInProgress = true;
                        arg0.startResolutionForResult(this, RC_SIGN_IN);
                 } catch (SendIntentException e) {
                       // The intent was canceled before it was sent. Return to the
                       // default
                       // state and attempt to connect to get an updated
                       // ConnectionResult.
                       mIntentInProgress = false;
                       mGoogleApiClient.connect();
                 }

          }

          // Log.e("error", "error code" + arg0.getResolution());
          Toast.makeText(this, "User is onConnectionFailed!", Toast.LENGTH_LONG)
                       .show();
   }

   @Override
   protected void onActivityResult(int requestCode, int responseCode,
                 Intent intent) {
          if (requestCode == RC_SIGN_IN) {
                 mIntentInProgress = false;
                 if (!mGoogleApiClient.isConnecting()) {
                       mGoogleApiClient.connect();
                 }
          }
   }

   @Override
   public void onConnected(Bundle arg0) {

          Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
          signInBtn.setVisibility(View.GONE);
          if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                 Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                 textView.setText("Welcome : " + person.getDisplayName());
                 try {
                       JSONObject jsonObject = new JSONObject(person.getImage()
                                     .toString());
                       String imageUrl = jsonObject.getString("url");

                       try {
                              URL url = new URL(imageUrl);
                              Bitmap bmp;
                              bmp = BitmapFactory.decodeStream(url.openConnection()
                                            .getInputStream());
                              profileImage.setImageBitmap(bmp);
                       } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        }

                 } catch (JSONException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                 }

          }

   }

   @Override
   public void onConnectionSuspended(int arg0) {

          Toast.makeText(this, "User is onConnectionSuspended!",
                       Toast.LENGTH_LONG).show();
   }

}

activity_main.xml中

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:padding="20dip">
<com.google.android.gms.common.SignInButton
    android:id="@+id/sign_in_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dip"
    />
 <TextView
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="9pt"
    android:text="" />

<ImageView
    android:id="@+id/profileImage"
    android:layout_width="200dp"
    android:layout_height="200dp"
    />
 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

如LogCat中所述,您需要添加SignInButton。根据您提供的文档,Google说:

  

您应该使用带有Google+徽标的红色登录按钮。

您实际在活动中添加了该按钮吗?