无法初始化 AccountKit android 工作室。初始化错误 501
Posted
技术标签:
【中文标题】无法初始化 AccountKit android 工作室。初始化错误 501【英文标题】:unable to Initialize AccountKit android studio. Initialization Error 501 【发布时间】:2016-06-09 20:14:20 【问题描述】:我想使用 AccountKit 创建一个简单的应用程序,它会在成功登录时显示“hello world”消息。 “activity_main.xml”包含一个简单的“hello world”消息。语法没有错误,但是当我尝试在手机上运行此程序时,我收到有关帐户工具包 sdk 初始化的错误。我已经在给定的here 中清楚地初始化了 LoginActivity 中的 accountkit sdk。没有与 AccountKit 类关联的名为 initializeSdk() 的函数。请帮帮我。
11-21 03:36:49.798 26469-26469/? E/androidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfocom.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_
android:layout_>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_email"
android:layout_
android:layout_
android:layout_margin="16dp"
android:clickable="true" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_sms"
android:layout_
android:layout_
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 代码取自:https://medium.com/@AndroidAdvance/facebook-sms-confirmation-documentation-for-android-is-full-of-dog-shit-fdddef051523#.tm6wmqos8
【问题讨论】:
【参考方案1】:创建一个名为 DemoApplication 的新类或任何您喜欢的扩展 Application 的名称。在您的清单文件中注册此类,如下面的代码所示。然后在 DemoApplication.java 文件的 onCreate() 中初始化 Fb Account 工具包
public class DemoApplication extends Application
@Override
public void onCreate()
super.onCreate();
AccountKit.initialize(getApplicationContext());
然后在 Mainifest 的 Application 元素中注册这个类
<?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>
【讨论】:
你能解释一下吗? @尼丁以上是关于无法初始化 AccountKit android 工作室。初始化错误 501的主要内容,如果未能解决你的问题,请参考以下文章
Facebook AccountKit 在主要活动上覆盖 onActivityResult