gpt4 book ai didi

java - Android MD5 实现崩溃

转载 作者:行者123 更新时间:2023-12-02 08:02:44 25 4
gpt4 key购买 nike

每当在 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

最佳答案

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

将您的 md5android 类重命名为 Md5androidActivity 并且崩溃应该会消失,因为它将与您 list 中的内容匹配。

关于java - Android MD5 实现崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8663430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com