0

I had created a simple message passing application in android studio to include this code into my Project work.But I was not able to switch the activity by passing a string into another activity.please help me out to find the problem.

When the button is triggered the application got ended itself instead of switching the activity.I had tried many ways to figure it out.

First Activity(MainActivity)

public class MainActivity extends AppCompatActivity {

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

        Button btn;
        EditText text;
        btn = (Button)findViewById(R.id.button);
        text = (EditText)findViewById(R.id.editText);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mess = text.getText().toString();
                Intent i= new Intent(MainActivity.this,Main2Activity.class);
                intent.putExtra("EXTRA",mess);
                startActivity(i);
            }
        });
    }
}

Second Activity(Main2Activity)

public class Main2Activity extends AppCompatActivity {

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

       EditText editText  = (EditText)findViewById(R.id.editText2);
       editText.setText(getIntent().getStringExtra("EXTRA"));
    }
}

The expected output was to exchange the data from one activity to another, but now the application gets stopped.

Harisankar
  • 17
  • 3
  • 1
    If by "the application closes itself", you mean it's crashing, please [edit] your question to provide the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Aug 15 '19 at 15:06
  • Thank you to specify the proper words. – Harisankar Aug 15 '19 at 15:17
  • 1
    @MikeM. isn't only "specifying the proper words", he's also asking for more critical information, information that you don't appear to have provided yet. – Hovercraft Full Of Eels Aug 15 '19 at 16:05
  • @HovercraftFullOfEels. I had specified that the application getting stopped by itself. There were no error detected in android studio. And while running the application When the button gets triggered it closes the app " without " giving "Unfortunately,Application has stopped. " . – Harisankar Aug 16 '19 at 03:44

0 Answers0