【问题标题】:Android AsyncTask ExceptionInIntilizationErrorAndroid AsyncTask ExceptionInIntilizationError
【发布时间】:2012-07-19 15:18:09
【问题描述】:

我正在尝试创建一个基本的 AsyncTask 以从密钥库中获取证书。

由于某种原因,即使是最简单的 AsyncTask 也不适合我,我不断收到有关 ExceptionInIntilizationError 的错误。

这是我用来让基本异步任务工作的测试代码:

public class Authenticator extends Activity {

PrivateKey privateKey = null;
String SavedAlias = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getCertificates("TEST");

    doSomething();

}

public void doSomething()
{
    new additionalStuff().execute();
}

public class AliasLoader extends AsyncTask<Void, Void, X509Certificate[]> 
{
    X509Certificate[] chain = null;

    @Override protected X509Certificate[] doInBackground(Void... params) {
        android.os.Debug.waitForDebugger();

        if(!SavedAlias.isEmpty())
        {
                try {
                    chain = KeyChain.getCertificateChain(getApplicationContext(),SavedAlias);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                }
        }
        else
        {
            this.cancel(true);
        }

        return chain;
    }
    @Override protected void onPostExecute(X509Certificate[] chain) 
    {
        try {
            privateKey = KeyChain.getPrivateKey(getApplicationContext(), SavedAlias);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
        }

        if (privateKey != null) {
            Signature signature = null;
            try {
                signature = Signature.getInstance("SHA1withRSA");
            } catch (NoSuchAlgorithmException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
            try {
                signature.initSign(privateKey);
            } catch (InvalidKeyException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

public void getCertificates(String Host)
{
    KeyChainAliasCallback callBack = new KeyChainAliasCallback() {

        @Override
        public void alias(String alias) {
            if (alias != null) 
            {
                saveAlias(alias);
                //doSomething();
            }
        }
    };

    KeyChain.choosePrivateKeyAlias(this, callBack,
    new String[] {"RSA", "DSA"}, // List of acceptable key types. null for any
    null,                        // issuer, null for any
    null,      // host name of server requesting the cert, null if unavailable
    443,                         // port of server requesting the cert, -1 if unavailable
    null);                       // alias to preselect, null if unavailable

}

public void saveAlias(String alias)
{
    SavedAlias = alias;
}

public class additionalStuff extends AsyncTask<String, String, Void>
{
    String test = "This is a Test!!!";

    @Override
    protected void onPreExecute() 
    {
    }
    @Override
    protected Void doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) 
    {
            super.onProgressUpdate(values);
    }
    @Override
    protected void onPostExecute(Void unused) 
    {
        Log.d("DEEPAM", "THIS IS A TEST");
        return;
    }

}
}

为什么会失败? =(

** 更新 ** 我进行了更改以在执行后添加 @Override 和参数 Void Void ,它仍然出现相同的 ExceptionInInitilizationError =(

** 更新 **

我猜我无法在 UI 中使用 Asynctask 创建一个新线程,这可能是它之前失败的原因,无论如何我可以在用户选择证书后调用 AsyncTask 吗?

【问题讨论】:

  • 可能受保护的 void onPostExecute(Void Void)(忘记参数)
  • 你能发布堆栈跟踪吗?另外,请遵守命名约定,即使用AdditionalStuff 而不是additionalStuff 作为类名。
  • 哦,我相信应该是protected void onPostExecute(Void unused)

标签: android android-asynctask


【解决方案1】:

为什么你的 onPostExecute 没有标签“@override”和任何参数?

【讨论】:

    【解决方案2】:

    我发现了问题,一旦我最初的错误得到解决,whatappserv、Vladimir 和 n_benbourahla 提到的内容可能会在以后出现问题。

    基本上我忘了添加一些正在使用执行的代码。 我在 KeyChain.choosePrivateKeyAlias 的回调中调用 doSomething.execute()。

    它看起来像是把它放在外面并且 asynctask 工作正常。

    我仍然有如何从 keychain.choosePrivateAlias 调用回调中的异步任务的问题,因为它必须只在执行后运行。我会用我现在拥有的更新代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-09
      • 2014-07-21
      • 2012-11-16
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多