下面的程序试图从第一个编辑框中获取单个文本并匹配单词 vysh 并在第二个编辑框中生成输出。第二个编辑框最初有点,如果每个单词都与单词 VYSH 匹配,那么它将替换点。
运行程序时出现错误,错误如下:
09-14 19:47:25.593: D/AndroidRuntime(469): Shutting down VM
09-14 19:47:25.593: W/dalvikvm(469): threadid=1: thread exiting with uncaught exception (group=0x40014760)
09-14 19:47:25.593: E/AndroidRuntime(469): FATAL EXCEPTION: main
09-14 19:47:25.593: E/AndroidRuntime(469): java.lang.NullPointerException
09-14 19:47:25.593: E/AndroidRuntime(469): at java.lang.String.<init>(String.java:505)
09-14 19:47:25.593: E/AndroidRuntime(469): at com.example.testinput.MainActivity$1.onClick(MainActivity.java:46)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.view.View.performClick(View.java:3110)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.view.View$PerformClick.run(View.java:11934)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.os.Handler.handleCallback(Handler.java:587)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.os.Looper.loop(Looper.java:132)
09-14 19:47:25.593: E/AndroidRuntime(469): at android.app.ActivityThread.main(ActivityThread.java:4123)
09-14 19:47:25.593: E/AndroidRuntime(469): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 19:47:25.593: E/AndroidRuntime(469): at java.lang.reflect.Method.invoke(Method.java:491)
09-14 19:47:25.593: E/AndroidRuntime(469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-14 19:47:25.593: E/AndroidRuntime(469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-14 19:47:25.593: E/AndroidRuntime(469): at dalvik.system.NativeStart.main(Native Method)
09-14 19:47:30.175: I/Process(469): Sending signal. PID: 469 SIG: 9
该程序如下图所示:
package com.example.testinput;
public class MainActivity extends Activity {
String rword="vysh";
private StringBuffer gword=new StringBuffer();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button1);
final EditText output=(EditText) findViewById(R.id.editText2);
//input dots in edittext
char positions[] = new char[rword.length()];
for (int i=0; i<rword.length(); i++) {
positions[i] = '.';
}
String dots=new String(positions);
output.setText(dots);
//end of input dots
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
EditText text=(EditText)findViewById(R.id.editText1);
String str="";
str=text.getText().toString();
if(!TextUtils.isEmpty(str))
{
//String t=new String(gword);
char a=str.charAt(0);
//replace dots with text
for (int i=0; i<rword.length(); i++)
{
if((rword.charAt(i))==a)
{
gword.setCharAt( i, a);
}
}
//print the output
output.setText(gword);
}end of iftextutils
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;}
}
尝试初始化字符串缓冲区并根据以下解决方案编辑程序,但程序不起作用...我收到以下错误:09-17 10:48:11.169: 09-17 11:36:03.439: E/AndroidRuntime(926 ): java.lang.StringIndexOutOfBoundsException: index=0 length=0 09-17 11:36:03.439: E/AndroidRuntime(926): at java.lang.AbstractStringBuilder.indexAndLength(AbstractStringBuilder.java:214) 09-17 11: 36:03.439: E/AndroidRuntime(926): 在 java.lang.AbstractStringBuilder.setCharAt(AbstractStringBuilder.java:544) 09-17 11:36:03.439: E/AndroidRuntime(926): 在 java.lang.StringBuffer.setCharAt (StringBuffer.java:698) 09-17 11:36:03.439: E/AndroidRuntime(926): 在 com.example.testinput.MainActivity$1.onClick(MainActivity.java:60) 09-17 11:36:03.439: E/AndroidRuntime(926):在 android.view.View.performClick(View.java:3110)
它显示 String.charat 函数存在错误。如果它与单词 vysh 匹配,基本上我想将编辑框中的点替换为第一个框中的字符。