简体   繁体   中英

unable to Initialize AccountKit android studio. Initialization Error 501

I wanted to create a simple app using AccountKit which shows "hello world" message upon successful login. "activity_main.xml" contains a simple "hello world" message. There were no errors in syntax but when i try to run this on my phone i am getting this error regarding account kit sdk initialization. I have clearly initialized the accountkit sdk in LoginActivity as given here . There is no function called initializeSdk() associated with AccountKit class. Please Help Me.

 11-21 03:36:49.798 26469-26469/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sya.zero/com.example.sya.zero.LoginActivity}: 500: Initialization error: 501: The SDK has not been initialized, make sure to call AccountKit.initializeSdk() first
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
                                                       at android.app.ActivityThread.access$600(ActivityThread.java:145)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                       at android.os.Looper.loop(Looper.java:137)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5099)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: 500: Initialization error: 501: The SDK has not been initialized, make sure to call AccountKit.initializeSdk() first
                                                       at com.facebook.accountkit.internal.Validate.sdkInitialized(Validate.java:82)
                                                       at com.facebook.accountkit.internal.Initializer.getAccessTokenManager(Initializer.java:176)
                                                       at com.facebook.accountkit.internal.AccountKitController.getCurrentAccessToken(AccountKitController.java:543)
                                                       at com.facebook.accountkit.AccountKit.getCurrentAccessToken(AccountKit.java:201)
                                                       at com.example.sya.zero.LoginActivity.onCreate(LoginActivity.java:43)
                                                       at android.app.Activity.performCreate(Activity.java:5117)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236) 
                                                       at android.app.ActivityThread.access$600(ActivityThread.java:145) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                       at android.os.Looper.loop(Looper.java:137) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5099) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:511) 

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true" />

</LinearLayout>

LoginActivity.java

package com.example.sya.zero;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.example.sya.zero.R;
import com.facebook.FacebookSdk;
import com.facebook.accountkit.AccessToken;
import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.AccountKitLoginResult;
import com.facebook.accountkit.AccountKitSdkVersion;
import com.facebook.accountkit.ui.AccountKitActivity;
import com.facebook.accountkit.ui.AccountKitConfiguration;
import com.facebook.accountkit.ui.LoginType;

public class LoginActivity extends AppCompatActivity {

    public static int APP_REQUEST_CODE = 99;

    private FloatingActionButton fab_email;
    private FloatingActionButton fab_sms;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        AccountKit.initialize(getApplicationContext());
        boolean x = AccountKit.isInitialized();
        System.out.println("hello" + "--" + x);
        setContentView(R.layout.activity_login);


        fab_email = (FloatingActionButton) findViewById(R.id.fab_email);
        fab_sms = (FloatingActionButton) findViewById(R.id.fab_sms);

        //-------- is it already logged in ?
        AccessToken accessToken = AccountKit.getCurrentAccessToken();
        if (accessToken != null) {
            Log.d("BaseApplication", ">>>>>>>>>>>> Handle Returning User with token " + String.valueOf(accessToken.toString()));
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        } else {
            Log.w("BaseApplication", ">>>>>>>>>>>> Handle new or logged out user");
        }

        fab_sms.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {

                final Intent intent = new Intent(LoginActivity.this, AccountKitActivity.class);
                AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                        new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE, AccountKitActivity.ResponseType.CODE).setReadPhoneStateEnabled(true); // or .ResponseType.TOKEN
                // ... perform additional configuration ...
                intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, configurationBuilder.build());
                startActivityForResult(intent, APP_REQUEST_CODE);
            }
        });

        fab_email.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                final Intent intent = new Intent(LoginActivity.this, AccountKitActivity.class);
                AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                        new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.EMAIL,
                                AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN
                // ... perform additional configuration ...
                intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, configurationBuilder.build());
                startActivityForResult(intent, APP_REQUEST_CODE);
            }
        });
    }



    @Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == APP_REQUEST_CODE) { // confirm that this response matches your request
            AccountKitLoginResult loginResult = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
            String errorMessage;
            if (loginResult.getError() != null) {
                errorMessage = loginResult.getError().getErrorType().getMessage();
                // or show a dialog / fragment etc
                Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_LONG).show();
            } else if (loginResult.wasCancelled()) {
                errorMessage = "Login Cancelled";
            } else {
                if (loginResult.getAccessToken() != null) {
                    errorMessage = "Success:" + loginResult.getAccessToken().getAccountId();
                } else {
                    errorMessage = String.format("Success:%s...", loginResult.getAuthorizationCode().substring(0, 10));
                }

                // If you have an authorization code, retrieve it from
                // loginResult.getAuthorizationCode()
                // and pass it to your server and exchange it for an access token.

                // Success! Start your next activity...
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
            }

            Snackbar.make(findViewById(android.R.id.content), errorMessage, Snackbar.LENGTH_LONG).show();
        }
    }
}

MainActivity.java

package com.example.sya.zero;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.sya.zero.R;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

login_activity code taken from : https://medium.com/@AndroidAdvance/facebook-sms-confirmation-documentation-for-android-is-full-of-dog-shit-fdddef051523#.tm6wmqos8

Create a new Class named as DemoApplication or any your prefer name that extends Application. Register this class in your Manifest file as shown in code below. then initialize the Fb Account kit in onCreate() on DemoApplication.java file

public class DemoApplication extends Application {

  @Override
  public void onCreate() {
       super.onCreate();
       AccountKit.initialize(getApplicationContext());
  } 
}

and there register this class in your Mainifest's Application element

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.androidyug.demoaccountkit">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:name=".DemoApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.facebook.accountkit.ui.AccountKitActivity" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />
    <meta-data
        android:name="com.facebook.accountkit.ClientToken"
        android:value="@string/ACCOUNT_KIT_CLIENT_TOKEN" />
    <meta-data
        android:name="com.facebook.accountkit.ApplicationName"
        android:value="@string/app_name" />

    <activity android:name=".HomeActivity"></activity>
</application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM