问题描述
- 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]) ; }
}
时间: 2024-11-08 18:17:48