Reputation:
I am working on a quiz app, and right now I have two XML files. One is the layout for the startscreen and the other XML file is the layout for the quiz. The quiz actually works, when I set the contentview
to my quizlayout
, but I obviously want to have the startlayout
, at the launch. Whenever I set the contentView
in the onCreate
method to my startlayout
, my handy closes the app.
QuizActivity (MainActivity):
package at.lorenzdirry.philosophenquiz;
import at.lorenzdirry.philosophenquiz.R.id;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class QuizActivity extends Activity implements android.view.View.OnClickListener {
Spiellogik spiel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Spiellogik instanziieren
spiel = new Spiellogik();
// Antwort-Buttons mit Ereignislistener verbinden
for (int n = 1; n <= 4; n++) {
Button btn = null;
switch (n) {
case 1:
btn = (Button) this.findViewById(id.antwort1);
btn.setOnClickListener(this);
break;
case 2:
btn = (Button) this.findViewById(id.antwort2);
btn.setOnClickListener(this);
break;
case 3:
btn = (Button) this.findViewById(id.antwort3);
btn.setOnClickListener(this);
break;
case 4:
btn = (Button) this.findViewById(id.antwort4);
btn.setOnClickListener(this);
break;
}
}
// 1. Frage laden
spiel.fragen[spiel.aktFrage].anzeigen(this);
}
public void onClick(View v) {
int id = v.getId();
if (id == R.id.antwort1)
spiel.auswerten(1, this); // spielAuswerten(1);
else if (id == R.id.antwort2)
spiel.auswerten(2, this); // spielAuswerten(2);
else if (id == R.id.antwort3)
spiel.auswerten(3, this); // spielAuswerten(3);
else if (id == R.id.antwort4)
spiel.auswerten(4, this); // spielAuswerten(4);
}
}
activity_main(startlayout):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/bg1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:padding="25dp"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="190dp"
android:id="@+id/imageView4"
android:layout_weight="2"
android:background="@drawable/coverschrift"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:layout_weight="1"
android:background="@drawable/rechterphilo"/>
</LinearLayout>
<Button
android:id="@+id/startb1"
android:text="Starte das Quiz"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/startb_custom"
android:shadowColor="#6AA7AB"
android:shadowDx="5"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginBottom="25dp"
android:textAllCaps="false"
/>
<Button
android:id="@+id/startb2"
android:layout_weight="1"
android:text="Informationen zu den Philosophen"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/startb_custom"
android:shadowColor="#6AA7AB"
android:shadowDx="5"
android:shadowDy="0"
android:shadowRadius="5"
android:textAllCaps="false"
/>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".QuizActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I am not posting the two class files for the quiz, cause the quiz, like I said, works fine.
Upvotes: 1
Views: 308
Reputation: 559
Your problem is that in the onCreateMethod you refer to buttons (antwort1, antwort2 etc.), which are not in the start layout, but in the quiz layout. Of course, it will cause errors and crash. If you want to start with the start layout, then you should put this part
// Antwort-Buttons mit Ereignislistener verbinden
for (int n = 1; n <= 4; n++) {
Button btn = null;
switch (n) {
case 1:
btn = (Button) this.findViewById(id.antwort1);
btn.setOnClickListener(this);
break;
case 2:
btn = (Button) this.findViewById(id.antwort2);
btn.setOnClickListener(this);
break;
case 3:
btn = (Button) this.findViewById(id.antwort3);
btn.setOnClickListener(this);
break;
case 4:
btn = (Button) this.findViewById(id.antwort4);
btn.setOnClickListener(this);
break;
}
}
// 1. Frage laden
spiel.fragen[spiel.aktFrage].anzeigen(this);
in another function, where you change to the quiz layout.
In my opinion, however, the best would be to either use a different activity for the start layout or, if you want only one activity, to use fragments (1 fragment for the start layout and 1 fragment for the quiz layout).
Upvotes: 1