-1

下面的程序试图从第一个编辑框中获取单个文本并匹配单词 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 匹配,基本上我想将编辑框中的点替换为第一个框中的字符。

4

4 回答 4

1

在该行String t=new String(gword);中,变量gword为空,这会导致异常。据我所见,t无论如何您都没有使用,所以...删除该行,或t使用简单的new String().

于 2012-09-14T20:11:27.757 回答
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;
}    

}

于 2012-09-17T12:41:54.483 回答
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);

}
于 2012-09-17T11:07:09.357 回答
0

你得到一个NullPointerException因为你的gwordin 声明但未初始化。所以,初始化它,它会正常工作。执行以下操作:

private StringBuffer gword = new StringBuffer();

于 2012-09-14T23:26:02.280 回答