2

我想从 EditText 中获取字符串,然后从中读取单个字符。我试过以下代码:

String PT = pt.getText().toString(); 
int len = PT.length(); 
char chars[] = PT.toCharArray(); 
for(int i=0;i<len;i++)
{ Toast.makeText(encrypt.this,chars[i], 0).show(); }

但是我收到了强制关闭错误。我哪里错了?

我对这一切都很陌生!

这是 LogCat:

11-28 12:41:01.228: E/AndroidRuntime(1632):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-28 12:41:01.228: E/AndroidRuntime(1632):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 12:41:01.228: E/AndroidRuntime(1632):     at java.lang.reflect.Method.invoke(Method.java:507)
11-28 12:41:01.228: E/AndroidRuntime(1632):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-28 12:41:01.228: E/AndroidRuntime(1632):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-28 12:41:01.228: E/AndroidRuntime(1632):     at dalvik.system.NativeStart.main(Native Method)
11-28 12:46:43.688: W/KeyCharacterMap(1674): No keyboard for id 0
11-28 12:46:43.688: W/KeyCharacterMap(1674): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-28 12:46:54.442: W/ResourceType(1674): No package identifier when getting value for resource number 0x00000061
11-28 12:46:54.448: D/AndroidRuntime(1674): Shutting down VM
11-28 12:46:54.448: W/dalvikvm(1674): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-28 12:46:54.459: E/AndroidRuntime(1674): FATAL EXCEPTION: main
11-28 12:46:54.459: E/AndroidRuntime(1674): android.content.res.Resources$NotFoundException: String resource ID #0x61
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.content.res.Resources.getText(Resources.java:201)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.widget.Toast.makeText(Toast.java:258)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at com.project.DENCrypt.encrypt$1.onClick(encrypt.java:33)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.view.View.performClick(View.java:2485)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.view.View$PerformClick.run(View.java:9080)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.os.Handler.handleCallback(Handler.java:587)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.os.Looper.loop(Looper.java:123)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at java.lang.reflect.Method.invoke(Method.java:507)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-28 12:46:54.459: E/AndroidRuntime(1674):     at dalvik.system.NativeStart.main(Native Method)
4

5 回答 5

4
Toast.makeText(encrypt.this,String.valueOf(chars[i]), 0).show();

代替

Toast.makeText(encrypt.this,chars[i], 0).show();
于 2012-11-28T07:27:11.310 回答
3

试试这个:

Toast.makeText(encrypt.this,chars[i] + "", 0).show();

它与 Ram 的方法相同

于 2012-11-29T07:38:43.680 回答
1

将您的吐司消息更改为

Toast.makeText(encrypt.this,Character.toString(chars[i]), Toast.LENGTH_SHORT).show(); 

这里的问题在于 Toast 消息。Toast 不能显示字符。所以将字符转换为字符串并显示

于 2012-11-28T07:24:32.593 回答
0

chars[i]整数值。如果您仔细查看错误日志,您会看到

android.content.res.Resources$NotFoundException: String resource ID #0x61

(如果您传递整数参数,android 将查找具有此 Id 的资源)

您可以直接从字符串中打印字符:

Toast.makeText(encrypt.this,PT[i], 0).show();
于 2012-11-28T07:25:13.500 回答
0

从它读取数据时,编辑文本(布局资源)可能不可用。只需检查您是否能够获得任何空值

EditText edt1 = (EditText)findViewById(R.id.myEditView);

记录 edt1(通过 log、system.out.println);

于 2012-11-28T07:26:00.893 回答