I am new to Android development and I am trying to send data via EditText to TextView from an alert dialog inflated in the MainActivity to a different activity (NewGame.java). I have tried intent.putExtra (see below) however I am getting null pointer exceptions as it seems I cannot access the views from the dialog layout xml file.
I know there are a few other ways to create an alert dialog so perhaps this way is not best for what i am trying to achieve?
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button newGame, prevGames, newSquad, savedSquads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newGame = findViewById(R.id.buttonNewGame);
newGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
EditText enterTeam1, enterTeam2, enterDate, enterTime, enterLocation;
enterTeam1 = myView.findViewById(R.id.etTeam1);
enterTeam2 = myView.findViewById(R.id.etTeam2);
enterDate = myView.findViewById(R.id.etDate);
enterTime = myView.findViewById(R.id.etTime);
enterLocation = myView.findViewById(R.id.etLocation);
builder.setTitle("New Game")
.setCancelable(true)
.setPositiveButton("Start", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(MainActivity.this, NewGame.class);
intent.putExtra("TEAM1", enterTeam1.getText().toString());
intent.putExtra("TEAM2", enterTeam2.getText().toString());
intent.putExtra("DATE", enterDate.getText().toString());
intent.putExtra("TIME", enterTime.getText().toString());
intent.putExtra("LOCATION", enterLocation.getText().toString());
startActivity(intent);
}
})
.setNegativeButton("Cancel", null);
builder.create();
builder.show();
}
});
NewGame.java
public class NewGame extends AppCompatActivity {
TextView team1, team2, date, time, location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
team1 = findViewById(R.id.tvTeam1);
team2 = findViewById(R.id.tvTeam2);
date = findViewById(R.id.tvDate);
time = findViewById(R.id.tvTime);
location = findViewById(R.id.tvLocation);
team1.setText(getIntent().getStringExtra("TEAM1"));
team2.setText(getIntent().getStringExtra("TEAM2"));
date.setText(getIntent().getStringExtra("DATE"));
time.setText(getIntent().getStringExtra("TIME"));
location.setText(getIntent().getStringExtra("LOCATION"));
}
}
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@ id/tvNewGame"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="@color/black"
android:textSize="30dp" />
<EditText
android:id="@ id/etTeam1"
android:layout_width="211dp"
android:layout_height="53dp"
android:ems="10"
android:hint="Team 1"
android:inputType="textPersonName" />
<EditText
android:id="@ id/etTeam2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Team 2"
android:inputType="textPersonName"/>
<EditText
android:id="@ id/etDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="@ id/etTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Time"
android:inputType="textPersonName" />
<EditText
android:id="@ id/etLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Location"
android:inputType="textPersonName"/>
</LinearLayout>
activity_new_game.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".NewGame">
<TextView
android:id="@ id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="time"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tvNewGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:textColor="@color/black"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.085" />
<TextView
android:id="@ id/tvTeam1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="team1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.192" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
I believe you are having this problem because you are actually inflating 2 views. And referencing the wrong one when you use findById
.
LayoutInflater inflater= getLayoutInflater();
builder.setView(R.layout.new_game_dialog);
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
should look like this
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.new_game_dialog, null);
builder.setView(myView);