我想用android工作室編碼詞彙應用程序。我有一個txt文件,其中包含UTF-8格式的詞彙。UTF-8編碼的日語字符不會出現在Android顯示
akarui _ あかるい _ bright
代碼讀取該文件,並添加到字典如下所示:
public Map<String, String> adjectives_ej = new HashMap<String, String>();
try {
InputStream in = am.open("adjectives_utf8.txt");
//BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null){
// printout first line
if (line != ""){
String[] parts = line.split("_");
byte[] bytes = parts[1].getBytes("UTF-8");
String japaneseString = new String(bytes, "UTF-8");
Log.d("voc", japaneseString);
adjectives_ej.put(parts[2].replaceAll(" ",""), new String(bytes, "UTF-8"));
adjectives_je.put(new String(bytes, "UTF-8"), parts[2].replaceAll(" ",""));
}
}
TextView textView = new TextView(this);
textView.setText(adjectives_ej.get("bright"));
ViewGroup layout = (ViewGroup)
findViewById(R.id.activity_adjectives);
layout.addView(textView);
如果我想看到的輸出我得到的錯誤信息:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ericwolf.genkiii/ericwolf.genkiii.Adjectives}: java.lang.NullPointerException: println needs a message
但是Log.d("voc", japaneseString);
給了我正確的輸出:07-31 19:42:41.600 25439-25439/ericwolf.genkiii D/voc: くらい
此外在「while」循環內設置textView.setText(parts[1]);
工作得很好。所以我不明白這裏的區別。將它保存在字典中有問題嗎?
使用'按住Shift鍵JIS',而不是'UTF-8'。希望它能解決你的問題。 – SkyWalker
可悲的不是:(同樣的錯誤,仍然沒有出現這個詞 –
可能是一個編碼問題,你能分享'adjectives_utf8.txt'嗎? – selbie