2 mozpp mozpp 于 2014.08.22 10:49 提问

Android 输入字符,转换为ascii码输出,编译没问题,但是运行时有问题

public class Activity1 extends Activity {

public static int char2ASCII(char c) {   
     return (int) c;   
 }   
 public static int[] string2ASCII(String s) {// 字符串转换为ASCII码   
        if (s == null || "".equals(s)) {   
            return null;   
        }   

        char[] chars = s.toCharArray();   
        int[] asciiArray = new int[chars.length];   

        for (int i = 0; i < chars.length; i++) {   
            asciiArray[i] = char2ASCII(chars[i]);   
        }   
        return asciiArray;   
    }   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity1);

    Button button1=(Button)this.findViewById(R.id.button1);
    final EditText editText1=(EditText)findViewById(R.id.editText1);



    button1.setOnClickListener(new OnClickListener()
    {
        public void onClick(View arg0)
        { 
            String a=editText1.getText().toString();
            Intent intentRef=new Intent();
            intentRef.setClass(Activity1.this, Activity2.class);

            Bundle bundle = new Bundle();
            bundle.putIntArray("a", string2ASCII(a));
            intentRef.putExtras(bundle);

            startActivity(intentRef);
            Activity1.this.finish();
        }
    }
            );
}

}`

public class Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity2);

    Bundle bundle = this.getIntent().getExtras();
    int[] b = bundle.getIntArray("a");

    TextView textview =(TextView) findViewById(R.id.textView1);
    textview.setText(b[0]) ;
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!