Domanda

I cant set text in an android textview item using an edittext:

 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);

        }

    };
    }

My app crashes. Please help.

È stato utile?

Soluzione

you just forgot

editText1 = (EditText) findViewById(R.id.editText1);

and thats why your app crashes

Altri suggerimenti

your app crashes because of nullpointer .This is because you are using editText1 without initializing it in onCreate

   editText1 = (EditText) findViewById(R.id.edittextID)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top