下面的程序试图从第一个编辑框中获取单个文本并匹配单词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;}
}
尝试初始化stringbuffer并根据以下解决方案编辑程序但程序不起作用...我收到以下错误: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):at java.lang.AbstractStringBuilder.setCharAt(AbstractStringBuilder.java:544) 09-17 11:36:03.439:E / AndroidRuntime(926):at java.lang.StringBuffer.setCharAt(StringBuffer.java:698) 09-17 11:36:03.439:E / AndroidRuntime(926):at 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匹配。
答案 0 :(得分:1)
在行String t=new String(gword);
中,变量gword
为空,这会导致异常。据我所知,您还没有使用t
,所以...删除该行,或使用简单的t
创建new String()
。
答案 1 :(得分:0)
您收到NullPointerException
,因为您的gword
已声明但尚未初始化。
所以,初始化它,它将工作正常。做类似的事情:
private StringBuffer gword = new StringBuffer();
答案 2 :(得分:0)
试试这个,
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);
}
答案 3 :(得分:0)
正确的代码是: package com.example.testinput; 公共类MainActivity扩展了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] = '.';gword.append(" ");
}
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
// to get input edibox
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 if textuitls
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}