首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过编辑文本将文本设置为文本视图

通过编辑文本将文本设置为文本视图
EN

Stack Overflow用户
提问于 2013-09-19 17:28:05
回答 2查看 1.3K关注 0票数 1

我不能使用编辑文本在android文本视图项目中设置文本:

代码语言:javascript
复制
 public class MainActivity extends Activity {
        TextView  textView1;
        private Button button1;
        private EditText editText1;
        private Editable e;
        private String ee;
        private SharedPreferences sh1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


        textView1 = (TextView) findViewById(R.id.textView1);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                saveM1(editText1.getText().toString());

            }

        });
        editText1.addTextChangedListener(eeeee);
        sh1 = getSharedPreferences("sh1",MODE_PRIVATE);

        }
        private void saveM1(String save1){

              SharedPreferences.Editor preferencesEditor = sh1.edit();
             preferencesEditor.putString("save1",save1); //change this line to this
             preferencesEditor.commit();

         }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        public boolean onOptionsItemSelected(MenuItem item){
            switch(item.getItemId())
            {
            case R.id.action_settings:
    Intent i = new Intent(this,aa.class);
    startActivity(i);

                return true;
            }
            return true;
        }
    public TextWatcher eeeee = new TextWatcher(){

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            e = editText1.getText(); //eeee
            textView1.setText("e"+e);

        }

    };
    }

我的应用程序崩溃了。请帮帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-19 17:30:22

你只是忘了

代码语言:javascript
复制
editText1 = (EditText) findViewById(R.id.editText1);

这就是为什么你的应用程序崩溃的原因。

票数 2
EN

Stack Overflow用户

发布于 2013-09-19 17:54:44

您的应用程序因为nullpointer .This而崩溃,是因为您使用的是editText1而没有在onCreate中初始化它。

代码语言:javascript
复制
   editText1 = (EditText) findViewById(R.id.edittextID)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18901276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档