1

When I'm trying to transfer data between activities, I can't get my message and the app keeps crashing, It show me 'app keeps stopping'

Code in MainActivity.java

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {

    Intent outIntent;
    EditText edtPhone;
    EditText edtMessage;
    Button btnNext;
    String tempText="";

   public static final String PHONE = "PHONE";
   public static final  String MESSAGE = "MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnNext = (Button) findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View V) {
                outIntent = new Intent(MainActivity.this, ActivityTwo.class);
                edtPhone = (EditText) findViewById(R.id.edtPhone);
                edtMessage = (EditText) findViewById(R.id.edtMessage);
                tempText = edtPhone.getText().toString();
                outIntent.putExtra(PHONE,tempText);
                tempText = edtMessage.getText().toString();
                outIntent.putExtra(MESSAGE,tempText);

                startActivity(outIntent);
            }

    });
}
    public void closeMethod(View view) {
        finish();
    }
}

ActivityTwo.java

package com.example.fir;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;

public class ActivityTwo extends AppCompatActivity {

        Intent incomingIntent;
        TextView txtPhone;
        TextView txtMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        incomingIntent = getIntent();
        txtPhone = (TextView) findViewById(R.id.txtPhone);
        txtMessage = (TextView) findViewById(R.id.txtMessage);
        txtPhone.setText(incomingIntent.getStringExtra(MainActivity.PHONE));
        txtMessage.setText(incomingIntent.getStringExtra(MainActivity.MESSAGE));

    }
}

Activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Phone" />

    <EditText
        android:id="@+id/editTextPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="60dp"
        android:ems="10"
        android:hint="Phone"
        android:inputType="phone"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="124dp"
        android:text="Message" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="3dp"
        android:ems="10"
        android:hint="Message"
        android:inputType=""
        android:minHeight="48dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="224dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="closeMethod"
            android:text="CLose" />
    </LinearLayout>
</RelativeLayout>
Activitytwo.xml
////

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Phone" />

    <EditText
        android:id="@+id/editTextPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="60dp"
        android:ems="10"
        android:hint="Phone"
        android:inputType="phone"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="124dp"
        android:text="Message" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="3dp"
        android:ems="10"
        android:hint="Message"
        android:inputType=""
        android:minHeight="48dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="224dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="closeMethod"
            android:text="CLose" />
    </LinearLayout>
</RelativeLayout>

The issue is when I don't put any value into the firstEditText or secondEditText or both of them and click on any button then the app crashes and a pop up shows "myapp keeps stopping". I cannot get the text and am unsure of why the app keeps crashing.

shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47
  • 2
    Welcome to StackOverflow. While showing code is important, also showing what errors you are getting is also important. Error message should be found in the logcat. See: [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/q/23353173/295004) and [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788/295004) on using a stack trace for debugging. – Morrison Chang Dec 25 '22 at 20:56

1 Answers1

0

you have defined your Edit texts in the first activity with id's named

edtPhone
edtMessage

but there was no edtPhone or edtMessage in your xml. this issue also repeats in your second activity you have defined txtPhone and txtMessage but again there are no such ids in your second activity XML. keep in mind that these might not be the whole problem as you didn't post any log for the errors. but if there were anything else, ask and we'll help you

Mohamad Rostami
  • 420
  • 3
  • 17