此代码重现了问题,仅此而已。仅执行一个 Activity 并创建AccountManger实例,然后调用getAccounts(),它就永远不会到达for (...行。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AccountManager manager = AccountManager.get(this);
        Account[] accounts = manager.getAccounts();
        for (int index = 0 ; index < accounts.length ; ++index)
            Log.i("RumbAPK", accounts[index].name);
    }
}

我要做的就是能够列出当前帐户并检查其是否经过身份验证,然后将其用作我的应用程序的身份验证系统。

编辑:

@ CommonsWave的请求,当然,因为它必须有用。

FATAL EXCEPTION: main
java.lang.SecurityException: caller uid 10085 lacks any of android.permission.GET_ACCOUNTS
    at android.os.Parcel.readException(Parcel.java:1428)
    at android.os.Parcel.readException(Parcel.java:1382)
    at android.accounts.IAccountManager$Stub$Proxy.getAccountsAsUser(IAccountManager.java:541)
    at android.accounts.AccountManager.getAccountsByTypeAsUser(AccountManager.java:414)
    at android.accounts.AccountManager.getAccountsByType(AccountManager.java:408)
    at com.har.innovation.and.technology.rumbapk.GoogleLoginManager.authenticate(GoogleLoginManager.java:31)
    at com.har.innovation.and.technology.rumbapk.MainActivity.onClick(MainActivity.java:132)
    at android.view.View.performClick(View.java:4220)
    at android.view.View$PerformClick.run(View.java:17510)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5455)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733)
    at dalvik.system.NativeStart.main(Native Method)

最佳答案

从堆栈跟踪中,我非常确定您缺少GET_ACCOUNTS中的AndroidManifest.xml权限

请添加以下行:

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

AndroidManifest.xml
有关android权限系统的更多Informations

08-26 00:51