-1

在问这个问题之前我尝试了知识库,但我没有找到任何相关的东西。

我正在 Android 平台上构建本机应用程序。我希望这个应用程序的用户能够使用他们的 Facebook 或 Google 登录详细信息登录。我的应用中有主要活动称为StartingPoint.java

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

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

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner.*;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook.*;
import com.facebook.android.FacebookError;
import com.google.android.gms.common.*;
import com.google.android.gms.common.GooglePlayServicesClient.*;
import com.google.android.gms.plus.PlusClient;

@SuppressWarnings("deprecation")
public class StartingPoint extends Activity 
implements  View.OnClickListener, ConnectionCallbacks, OnConnectionFailedListener{

    private static final String TAG = "StartingPoint";
    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private ConnectionResult mConnectionResult;

    public static String APP_ID = "**************";
    // Instance of Facebook Class
     private Facebook facebook = new Facebook(APP_ID);
     private AsyncFacebookRunner mAsyncRunner;
     String FILENAME = "AndroidSSO_data";
     private SharedPreferences mPrefs;

     TextView signup;
    //Buttons
     Button btnfacebook, google;

     @SuppressWarnings("deprecation")
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPlusClient = new PlusClient.Builder(this, this, this)
        .setVisibleActivities("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
        .build();
         // Progress bar to be displayed if the connection failure is not resolved.
        mConnectionProgressDialog = new ProgressDialog(this);
        mConnectionProgressDialog.setMessage("Signing in...");
        setContentView(R.layout.login);
        signup = (TextView) findViewById(R.id.textView5);
        btnfacebook = (Button) findViewById(R.id.loginfb);
        google = (Button) findViewById(R.id.loging);
        google.setOnClickListener(this);
        mAsyncRunner = new AsyncFacebookRunner(facebook);


         btnfacebook.setOnClickListener(new View.OnClickListener() {

               @Override
               public void onClick(View v) {
                Log.d("Image Button", "button Clicked");
                loginToFacebook();

               }
              });
        /*google.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                WebView myWebView = (WebView) findViewById(R.id.webview);
                myWebView.loadUrl("http://www.aristoslabs.com");
                // AlertDialog alertDialog = new
                // AlertDialog.Builder(StartingPoint.this).create();
                // alertDialog.setTitle("Welcome to Colletor");
                // alertDialog.setMessage("We will be back soon");
                // alertDialog.show();
            }
        });*/
    }
     @Override
        protected void onStart() {
            super.onStart();
            mPlusClient.connect();
        }

     @Override
        protected void onStop() {
            super.onStop();
            mPlusClient.disconnect();
        }
     @Override
        public void onConnectionFailed(ConnectionResult result) {
          if (mConnectionProgressDialog.isShowing()) {
            // The user clicked the sign-in button already. Start to resolve
            // connection errors. Wait until onConnected() to dismiss the
            // connection dialog.
            if (result.hasResolution()) {
              try {
                       result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
               } catch (SendIntentException e) {
                       mPlusClient.connect();
               }
            }
          }
          // Save the result and resolve the connection failure upon a user click.
          mConnectionResult = result;
        }

        protected void onActivityResult1(int requestCode, int responseCode, Intent intent) {
            if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
                mConnectionResult = null;
                mPlusClient.connect();
            }
        }

        @Override
        public void onConnected(Bundle connectionHint) {
            String accountName = mPlusClient.getAccountName();
            Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
        }
        @Override
        public void onDisconnected() {
            Log.d(TAG, "disconnected");
        }


     /**
      * Function to login into facebook
      * */
     @SuppressWarnings("deprecation")
     public void loginToFacebook() {

      mPrefs = getPreferences(MODE_PRIVATE);
      String access_token = mPrefs.getString("access_token", null);
      long expires = mPrefs.getLong("access_expires", 0);

      if (access_token != null) {
       facebook.setAccessToken(access_token);
       Intent i = new Intent();
        i.setClass(StartingPoint.this, Settings.class);
        startActivity(i);
       /* *********************************************** */
        btnfacebook.setVisibility(View.INVISIBLE);

       // Making get profile button visible
      // btnFbGetProfile.setVisibility(View.VISIBLE);

       // Making post to wall visible
       //btnPostToWall.setVisibility(View.VISIBLE);

       // Making show access tokens button visible
       //btnShowAccessTokens.setVisibility(View.VISIBLE);

       Log.d("FB Sessions", "" + facebook.isSessionValid());
      }

      if (expires != 0) {
       facebook.setAccessExpires(expires);
      }

      if (!facebook.isSessionValid()) {
       facebook.authorize(this,
         new String[] { "email", "publish_stream" },
         new DialogListener() {

          @Override
          public void onCancel() {
           // Function to handle cancel event
          }

          @Override
          public void onComplete(Bundle values) {
           // Function to handle complete event
           // Edit Preferences and update facebook acess_token
           SharedPreferences.Editor editor = mPrefs.edit();
           editor.putString("access_token",
             facebook.getAccessToken());
           editor.putLong("access_expires",
             facebook.getAccessExpires());
           editor.commit();
                   Intent i = new Intent();
            i.setClass(StartingPoint.this, Settings.class);
            startActivity(i);


           // Making Login button invisible
          // btnfacebook.setVisibility(View.INVISIBLE);

           // Making logout Button visible
           //btnFbGetProfile.setVisibility(View.VISIBLE);

           // Making post to wall visible
           //btnPostToWall.setVisibility(View.VISIBLE);

           // Making show access tokens button visible
           //btnShowAccessTokens.setVisibility(View.VISIBLE);
          }

          @Override
          public void onError(DialogError error) {
           // Function to handle error

          }

          @Override
          public void onFacebookError(FacebookError fberror) {
           // Function to handle Facebook errors

          }

         });
      }
     }

     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      facebook.authorizeCallback(requestCode, resultCode, data);
     }
    /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.starting_point, menu);
        return true;
    }*/
     @Override
     public void onClick(View view) {
         if (view.getId() == R.id.loging && !mPlusClient.isConnected()) {
             if (mConnectionResult == null) {
                 mConnectionProgressDialog.show();
             } else {
                 try {
                     mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
                 } catch (SendIntentException e) {
                     // Try connecting again.
                     mConnectionResult = null;
                     mPlusClient.connect();
                 }
             }
         }
     }
}

当我运行我的应用程序时,基本上我的应用程序崩溃了。在日志报告之后,我进入了我的 logcat

09-18 18:22:52.750: D/libEGL(20862): loaded /system/lib/egl/libEGL_mali.so
09-18 18:22:52.760: D/libEGL(20862): loaded /system/lib/egl/libGLESv1_CM_mali.so
09-18 18:22:52.765: D/libEGL(20862): loaded /system/lib/egl/libGLESv2_mali.so
09-18 18:22:52.770: D/(20862): Device driver API match
09-18 18:22:52.770: D/(20862): Device driver API version: 10
09-18 18:22:52.770: D/(20862): User space API version: 10 
09-18 18:22:52.770: D/(20862): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
09-18 18:22:52.810: D/OpenGLRenderer(20862): Enabling debug mode 0
09-18 18:22:52.865: D/dalvikvm(20862): GC_CONCURRENT freed 149K, 8% free 12371K/13319K, paused 21ms+1ms, total 52ms
09-18 18:22:53.710: D/AndroidRuntime(20862): Shutting down VM
09-18 18:22:53.710: W/dalvikvm(20862): threadid=1: thread exiting with uncaught exception (group=0x411452a0)
09-18 18:22:53.725: E/AndroidRuntime(20862): FATAL EXCEPTION: main
09-18 18:22:53.725: E/AndroidRuntime(20862): java.lang.SecurityException: Missing android.permission.GET_ACCOUNTS
09-18 18:22:53.725: E/AndroidRuntime(20862):    at android.os.Parcel.readException(Parcel.java:1425)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at android.os.Parcel.readException(Parcel.java:1379)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.internal.ej$a$a.getAccountName(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.internal.ek.getAccountName(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.plus.PlusClient.getAccountName(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at        StartingPoint.onConnected(StartingPoint.java:136)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.internal.u.v(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.internal.u$f.a(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.internal.u$f.a(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.internal.u$b.A(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.google.android.gms.internal.u$a.handleMessage(Unknown Source)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at android.os.Looper.loop(Looper.java:137)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at android.app.ActivityThread.main(ActivityThread.java:4898)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at java.lang.reflect.Method.invoke(Method.java:511)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-18 18:22:53.725: E/AndroidRuntime(20862):    at dalvik.system.NativeStart.main(Native Method)
09-18 18:23:03.805: I/Process(20862): Sending signal. PID: 20862 SIG: 9

我知道这段代码中有一些不正确的地方,但我无法弄清楚。

请告诉我。提前致谢

4

2 回答 2

2

里面<manifest></manifest>这个节点:

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
于 2013-09-18T14:55:37.977 回答
0

这是你的线索。

    09-18 18:22:53.725: E/AndroidRuntime(20862): 
java.lang.SecurityException: Missing android.permission.GET_ACCOUNTS

您需要将此权限添加到清单中。

于 2013-09-18T13:20:40.073 回答