尝试启动新活动时应用程序崩溃

时间:2015-07-12 08:48:20

标签: java android

当我尝试启动一个acticity(图库)时,应用程序似乎因此错误而崩溃:

07-12 10:27:55.614: E/AndroidRuntime(12215): FATAL EXCEPTION: main
07-12 10:27:55.614: E/AndroidRuntime(12215): Process: com.map.example, PID: 12215
07-12 10:27:55.614: E/AndroidRuntime(12215): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.map.example/com.map.example.media.Gallery}: java.lang.IllegalStateException: calling this from your main thread can lead to deadlock
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.os.Looper.loop(Looper.java:136)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.ActivityThread.main(ActivityThread.java:5586)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at java.lang.reflect.Method.invokeNative(Native Method)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at java.lang.reflect.Method.invoke(Method.java:515)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at dalvik.system.NativeStart.main(Native Method)
07-12 10:27:55.614: E/AndroidRuntime(12215): Caused by: java.lang.IllegalStateException: calling this from your main thread can lead to deadlock
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.accounts.AccountManager.ensureNotOnMainThread(AccountManager.java:1423)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.accounts.AccountManager.access$200(AccountManager.java:145)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.accounts.AccountManager$Future2Task.internalGetResult(AccountManager.java:1673)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.accounts.AccountManager$Future2Task.getResult(AccountManager.java:1710)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at com.map.example.picasa.PicasaApi.getAccounts(PicasaApi.java:66)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at com.map.example.media.PicasaDataSource.getAccountStatus(PicasaDataSource.java:39)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at com.map.example.media.Gallery.onCreate(Gallery.java:168)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.Activity.performCreate(Activity.java:5451)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
07-12 10:27:55.614: E/AndroidRuntime(12215):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
07-12 10:27:55.614: E/AndroidRuntime(12215):    ... 11 more

我不知道如何解决这个问题,我试图按照SO上的建议实施一些修复,但没有运气。我希望有人可以帮我这个吗?

这是PicasaAPI.java:66

public static final class AuthAccount {
    public final String user;
    public final String authToken;
    public final Account account;

    public AuthAccount(String user, String authToken, Account account) {
        this.user = user;
        this.authToken = authToken;
        this.account = account;
    }
}

public static Account[] getAccounts(Context context) {
    // Return the list of accounts supporting the Picasa GData service.
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = {};
    try {
        accounts = accountManager.getAccountsByTypeAndFeatures(PicasaService.ACCOUNT_TYPE,
                new String[] { PicasaService.FEATURE_SERVICE_NAME }, null, null).getResult();
    } catch (OperationCanceledException e) {
    } catch (AuthenticatorException e) {
    } catch (IOException e) {
    }
    return accounts;
}

第66行是new String[] { PicasaService.FEATURE_SERVICE_NAME }, null, null).getResult();

然后是PicasaDataSource.java:39

public static final HashMap<String, Boolean> getAccountStatus(final Context context) {
    final Account[] accounts = PicasaApi.getAccounts(context);
    int numAccounts = accounts.length;
    HashMap<String, Boolean> accountsEnabled = new HashMap<String, Boolean>(numAccounts);
    for (int i = 0; i < numAccounts; ++i) {
        Account account = accounts[i];
        boolean isEnabled = ContentResolver.getSyncAutomatically(account, PicasaContentProvider.AUTHORITY);
        String username = account.name.toLowerCase();
        if (username.contains("@gmail.") || username.contains("@googlemail.")) {
            // Strip the domain from GMail accounts for canonicalization.
            // TODO: is there an official way?
            username = username.substring(0, username.indexOf('@'));
        }
        accountsEnabled.put(username, new Boolean(isEnabled));
    }
    return accountsEnabled;
}

第39行是:final Account[] accounts = PicasaApi.getAccounts(context);

最后是Gallery.java:168

} else {
                // View intent for images.
                Uri uri = getIntent().getData();
                boolean slideshow = getIntent().getBooleanExtra("slideshow", false);
                final SingleDataSource singleDataSource = new SingleDataSource(Gallery.this, uri.toString(), slideshow);
                final ConcatenatedDataSource singleCombinedDataSource = new ConcatenatedDataSource(singleDataSource, picasaDataSource);
                try {
                    mGridLayer.setDataSource(singleCombinedDataSource);
            mGridLayer.setViewIntent(true, Utils.getBucketNameFromUri(uri));
                } catch (NullPointerException e) {
                    Log.i(TAG,"NullPointerException");
                }
                if (singleDataSource.isSingleImage()) {
                    mGridLayer.setSingleImage(false);
                } else if (slideshow) {
                    mGridLayer.setSingleImage(true);
                    mGridLayer.startSlideshow();
                }
            }
        }
    };
    t.start();
    mAccountsEnabled = PicasaDataSource.getAccountStatus(this);
    Log.i(TAG, "onCreate");
}

private void showToast(final String string, final int duration) {
    mHandler.post(new Runnable() {
        public void run() {
            Toast.makeText(Gallery.this, string, duration).show();
        }
    });
}

第168行是:mAccountsEnabled = PicasaDataSource.getAccountStatus(this);

通过这样打破我认为它与帐户有关?

有人可以帮忙(现在已经被困在这里差不多两天了!:-()

由于

2 个答案:

答案 0 :(得分:1)

你尝试做一些阻止UI线程的工作。您需要在后台线程中运行此代码。请试试这段代码。

YourActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
       mAccountsEnabled = PicasaDataSource.getAccountStatus(this);
    }
});

答案 1 :(得分:1)

试试这个:

new Thread(new Runnable() {
        @Override
        public void run() {
            mAccountsEnabled = PicasaDataSource.getAccountStatus(YourActivity.this);
        }
    }).start();