I wanted to make a try-catch block.
In my try block there's a method which checks if a radioButton
is pressed, if so it should set an enum and go forward and if no radioButton
is pressed, a NullPointerException
should be thrown and the app closes (there're coming some extra features which aren't finished yet so I removed them for now).
Everything I saw so far didnt fit to my problem because they just forgot a catch or they missspelled a method or something like this.
It works if I don't press a radioButton
my app closes, but if I press one radioButton
my app closes again, but it should go to my next layout and set an enum (depending on which radioButton
is pressed.
My java-code:
public void setKnowledge() {
setContentView(R.layout.knowledge_level);
Button checking = (Button) findViewById(R.id.buttonChecking);
checking.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
try {
checkSelection();
setContentView(R.layout.category_auswahl);
}catch(NullPointerException n) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
});
}
public knowledge checkSelection() {
RadioButton rbNoob = (RadioButton) findViewById(R.id.radioButtonNoob);
RadioButton rbBeginner = (RadioButton) findViewById(R.id.radioButtonBeginner);
RadioButton rbAdvanced = (RadioButton) findViewById(R.id.radioButtonAdv);
RadioButton rbPro = (RadioButton) findViewById(R.id.radioButtonPro);
RadioButton rbGrandMaster = (RadioButton) findViewById(R.id.radioButtonGM);
advertisment();
if(rbNoob.isChecked()) {
return knowledge.NOOB;
}
else if(rbBeginner.isChecked()) {
return knowledge.BEGINNER;
}
else if(rbAdvanced.isChecked()) {
return knowledge.ADVANCED;
}
else if(rbPro.isChecked()) {
return knowledge.PRO;
}
else if(rbGrandMaster.isChecked()) {
return knowledge.GM;
}
return null;
}
My XML: (EDIT: knowledge_level.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#081929" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/testWissenstandText"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:id="@+id/radioGroup"
app:layout_constraintRight_toLeftOf="@+id/buttonChecking"
android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.538">
<RadioButton
android:id="@+id/radioButtonNoob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="8dp"
android:checked="false"
android:text="Keine Vorkenntnisse"
android:textAlignment="center"
android:textColor="#E0E0E0"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/buttonChecking"
app:layout_constraintTop_toBottomOf="@+id/testWissenstandText"/>
<RadioButton
android:id="@+id/radioButtonBeginner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="8dp"
android:checked="false"
android:text="Anfänger"
android:textAlignment="center"
android:textColor="#E0E0E0"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@+id/radioButtonNoob"
app:layout_constraintRight_toRightOf="@+id/radioButtonNoob"
app:layout_constraintTop_toBottomOf="@+id/radioButtonNoob"/>
<RadioButton
android:id="@+id/radioButtonAdv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="8dp"
android:text="Fortgeschrittener"
android:textAlignment="center"
android:textColor="#E0E0E0"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@+id/radioButtonBeginner"
app:layout_constraintRight_toRightOf="@+id/radioButtonBeginner"
app:layout_constraintTop_toBottomOf="@+id/radioButtonBeginner"/>
<RadioButton
android:id="@+id/radioButtonPro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="8dp"
android:text="Pro"
android:textAlignment="center"
android:textColor="#E0E0E0"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@+id/radioButtonAdv"
app:layout_constraintRight_toRightOf="@+id/radioButtonAdv"
app:layout_constraintTop_toBottomOf="@+id/radioButtonAdv"/>
<RadioButton
android:id="@+id/radioButtonGM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="8dp"
android:text='"Grand Master"'
android:textAlignment="center"
android:textColor="#E0E0E0"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="@+id/radioButtonPro"
app:layout_constraintRight_toRightOf="@+id/radioButtonPro"
app:layout_constraintTop_toBottomOf="@+id/radioButtonPro"/>
</RadioGroup>
<ImageView
android:id="@+id/kastenKnowledge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/kasten"/>
<TextView
android:id="@+id/testWissenstandHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Dein Wissensstand?"
android:textAlignment="center"
android:textColor="#511613"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/kastenKnowledge"
app:layout_constraintLeft_toLeftOf="@+id/kastenKnowledge"
app:layout_constraintRight_toRightOf="@+id/kastenKnowledge"
app:layout_constraintTop_toTopOf="@+id/kastenKnowledge"/>
<TextView
android:id="@+id/testWissenstandText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text='Wie würdest du deinen Wissenstand\nbezüglich dem Spieleinhalt\nvon "Overwatch" beurteilen?'
android:textAlignment="center"
android:textColor="#E0E0E0"
android:textSize="20sp"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/testWissenstandHeader"/>
<Button
android:id="@+id/buttonChecking"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="0dp"
android:background="@drawable/button_box"
android:text="C\nL\nI\nC\nK"
android:textAllCaps="false"
android:textColor="#511613"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="@+id/radioGroup"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.5"/>
<TextView
android:id="@+id/knowledgeHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:text="Wenn du einen Wissenstand\nfestgelegt hast, drücke den\nKnopf daneben!"
android:textAlignment="center"
android:textColor="#FF4400"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/radioGroup"/>
</android.support.constraint.ConstraintLayout>
Error message(s):
08-14 21:01:26.469 4312-4312/? I/Process: Sending signal. PID: 4312 SIG: 9
08-14 21:01:26.470 1521-1590/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
--------- beginning of system
08-14 21:01:26.476 1521-1773/system_process I/WindowState: WIN DEATH: Window{ab30a43 u0 com.example.alexander.hashtagnevereverbuggy/com.example.alexander.hashtagnevereverbuggy.MainActivity}
08-14 21:01:26.480 1521-2538/system_process I/ActivityManager: Process com.example.alexander.hashtagnevereverbuggy (pid 4312) has died
08-14 21:01:26.480 1521-2538/system_process W/ActivityManager: Force removing ActivityRecord{ed88b90 u0 com.example.alexander.hashtagnevereverbuggy/.MainActivity t137}: app died, no saved state
08-14 21:01:26.491 1128-1128/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
08-14 21:01:27.104 1776-1958/com.android.launcher D/EGL_emulation: eglMakeCurrent: 0x7fd4ff628200: ver 2 0
08-14 21:01:27.135 1521-1773/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 4312 uid 10058