自学Android 坑1

来源:互联网 发布:电子表格删除重复数据 编辑:程序博客网 时间:2024/06/05 15:22

闲来无事,想接触下android 在极客学员的wiki上看到http://wiki.jikexueyuan.com/project/android-training-geek/ 官方培训课程。

自己学习碰到很多坑,在这里记录下来,方便回顾。

public void sendMessage(View view) {    Intent intent = new Intent(this, DisplayMessageActivity.class);    EditText editText = (EditText) findViewById(R.id.edit_message);    String message = editText.getText().toString();    intent.putExtra(EXTRA_MESSAGE, message);    startActivity(intent);}
这里由于自己粗心 碰到了两个错误

public void sendMessage(View view) {    Intent intent = new Intent(this, DisplayMessageActivity.class);    EditText editText = (EditText) view.findViewById(R.id.edit_message);    //String message = editText.getText().toString();    intent.putExtra(EXTRA_MESSAGE, editText.getText());    startActivity(intent);}
错误1 findViewById的时候由于看到了参数有个view 就粗心的用到了。结果肯定是返回null。要找的view肯定是在这个activity中的。而参数里面的view是点击的button,与要找的view在同一级。并不能findViewById。

错误2 intentputExtra()的时候偷懒,直接传了editText.getText()。到第二个activity中

intent.getStringExtra(MyActivity.EXTRA_MESSAGE);取到的也是null。因为editText.getText()返回的Editable并不是String。所以要toString()。
刚接触Android 第一个小程序在自己手机跑起来的成就感挺棒的。加油!
自学项目地址github    https://github.com/LoveingGuosai/firstapp.git

0 0