有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

由于securityException导致java运行时异常

我正在使用此代码创建帐户。但它抛出了安全异常。并且该帐户没有被创建

public static Account getSyncAccount(Context context) {
    AccountManager accountManager =(AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account newAccount = new Account(context.getString(R.string.app_name), context.getString(R.string.sync_account_type));

    if ( null == accountManager.getPassword(newAccount) ) {
        if (!accountManager.addAccountExplicitly(newAccount, "", null))
            return null;                     
       onAccountCreated(newAccount, context);
    }
    return newAccount;
}

logcat中的错误如下所示:

02-24 22:00:03.722: E/AndroidRuntime(30319): FATAL EXCEPTION: main
02-24 22:00:03.722: E/AndroidRuntime(30319): Process: com.example.yogesh.smartshoppee, PID: 30319
02-24 22:00:03.722: E/AndroidRuntime(30319): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yogesh.smartshoppee/com.example.yogesh.smartshoppee.Activity.ShoppingListActivity}: java.lang.SecurityException: uid 10176 cannot get secrets for accounts of type: Save image
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.ActivityThread.-wrap11(ActivityThread.java)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.os.Handler.dispatchMessage(Handler.java:102)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.os.Looper.loop(Looper.java:148)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.ActivityThread.main(ActivityThread.java:5443)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at    java.lang.reflect.Method.invoke(Native Method)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-24 22:00:03.722: E/AndroidRuntime(30319): Caused by: java.lang.SecurityException: uid 10176 cannot get secrets for accounts of type: Save image
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.os.Parcel.readException(Parcel.java:1620)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.os.Parcel.readException(Parcel.java:1573)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.accounts.IAccountManager$Stub$Proxy.getPassword(IAccountManager.java:694)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.accounts.AccountManager.getPassword(AccountManager.java:334)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at com.example.yogesh.smartshoppee.sync.AppSyncAdapter.getSyncAccount(AppSyncAdapter.java:190)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at com.example.yogesh.smartshoppee.sync.AppSyncAdapter.initializeSyncAdapter(AppSyncAdapter.java:234)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at com.example.yogesh.smartshoppee.Activity.ShoppingListActivity.onCreate(ShoppingListActivity.java:58)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.Activity.performCreate(Activity.java:6245)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
02-24 22:00:03.722: E/AndroidRuntime(30319):    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
02-24 22:00:03.722: E/AndroidRuntime(30319):    ... 9 more

AndroidManifest如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.demo" >
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<uses-permission 安卓:name="安卓.permission.READ_SYNC_SETTINGS" />
<uses-permission 安卓:name="安卓.permission.WRITE_SYNC_SETTINGS" />
<uses-permission 安卓:name="安卓.permission.AUTHENTICATE_ACCOUNTS" />

<application
    安卓:name=".app.MyApplication"
    安卓:allowBackup="true"
    安卓:icon="@mipmap/ic_launcher"
    安卓:label="@string/app_name"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name=".Activity.LoginScreen1Activity"
        安卓:label="@string/app_name"
        安卓:windowSoftInputMode="stateHidden" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />
            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        安卓:name=".Activity.MainActivity"
        安卓:label="@string/app_name" >
    </activity>
    <activity
        安卓:name=".Activity.MyAccountActivity"
        安卓:label="@string/title_activity_my_account" >
    </activity>

    <service
        安卓:name="com.example.demo.sync.AppAuthenticatorService">
        <intent-filter>
            <action 安卓:name="安卓.accounts.AccountAuthenticator"/>
        </intent-filter>
        <meta-data
            安卓:name="安卓.accounts.AccountAuthenticator"
            安卓:resource="@xml/authenticator" />
    </service>


    <service
        安卓:name="com.example.demo.sync.AppSyncService"
        安卓:exported="true"
        安卓:process=":sync">
        <intent-filter>
            <action 安卓:name="安卓.content.SyncAdapter"/>
        </intent-filter>
        <meta-data 安卓:name="安卓.content.SyncAdapter"
            安卓:resource="@xml/syncadapter" />
    </service>
</application>

我在网上查了一下,但找不到解决办法。我做错了什么


共 (0) 个答案