Android MD5实施崩溃

时间:2011-12-29 02:32:39

标签: java android md5

每当在Android 2.1环境中运行以下代码时,它都会崩溃。以下代码用于将纯文本字符串转换为MD5。字符串s硬编码为test。它应该打印以下结果:098f6bcd4621d373cade4e832627b4f6

package md5.android;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; 
import java.util.ArrayList; 
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class md5android extends Activity {

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

        String s = "test";
        String  res = md5(s);
        TextView tv = new TextView(this);
        tv.setText(res);
        setContentView(tv);
    }

    public String md5(String s) { 
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();

            StringBuffer hexString = new StringBuffer();
            for (int i=0; i<messageDigest.length; i++)
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));

            return hexString.toString();

        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}

堆栈追踪:

Thread [<3> main] (Suspended (exception RuntimeException))    
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417    
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2512    
ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119    
ActivityThread$H.handleMessage(Message) line: 1863    
ActivityThread$H(Handler).dispatchMessage(Message) line: 99    
Looper.loop() line: 123    
ActivityThread.main(String[]) line: 4363    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]    
Method.invoke(Object, Object...) line: 521    
ZygoteInit$MethodAndArgsCaller.run() line: 860    
ZygoteInit.main(String[]) line: 618    
NativeStart.main(String[]) line: not available [native method]

错误日志:

http://pastebin.com/dHXHe9SC

1 个答案:

答案 0 :(得分:2)

该代码应该可以正常工作。从日志来看,问题是您的Activity类名称与您在AndroidManifest.xml中声明的名称不匹配。 Android尝试加载活动md5.android.Md5androidActivity,但实际活动为md5.android.md5android

将您的md5android课程重命名为Md5androidActivity,然后崩溃就会消失,因为它会与您在宣传单中的内容相匹配。