-5

i was trying to use strings to store the name into plainText..but when the porgramme starts and as soon as i tap the button after writing into editText the app stops

package com.example.anujt.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


EditText et;
TextView tv;




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


    Button bt= findViewById(R.id.butt);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String eString = (et.getText().toString() );
            tv.setText(eString);
        }
    });








}

}

and this is the string ...is this making it untranslatable ?

<resources>
<string name="app_name" translatable="false">My Application</string>
<string name="butt" translatable="false">Submit</string>
<string name="write_tour_name_here" translatable="false">Write tour name 
here</string>
<string name="textview" translatable="false">TextView</string>


</resources>

4 Answers4

2

EditText and TextView is not initialized. You have initialized button using "findViewById", but that is missing for the EditText and TextView.

Saili
  • 21
  • 3
1

You should do this before getting text from Edittext

et = findViewById(R.id.et);
tv = findViewById(R.id.tv);
Jyoti JK
  • 2,112
  • 1
  • 14
  • 39
Saurabh Vadhva
  • 624
  • 5
  • 12
1

Try this

You have not initialized Edittext.

Button bt= findViewById(R.id.butt);
et = findViewById(R.id.youredittextid);

bt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String eString = (et.getText().toString() );
        tv.setText(eString);
    }
});
Ratilal Chopda
  • 4,113
  • 4
  • 17
  • 29
1

you have to initialize every view before using it.

Android Studio 3.0+

et = findViewById(R.id.idOfEditText);
tv=findViewById(R.id.idOfTextView);

Android Studio < 3.0

et = (EditText)findViewById(R.id.idOfEditText);
tv=(TextView)findViewById(R.id.idOfTextView);
Zaid Mirza
  • 3,182
  • 1
  • 20
  • 39