Я, наверное, потратил около 10 часов на эту ошибку xD все еще не могу найти ее:/ У меня есть в основном 3 макета. Первый макет для меню/дома, второй - викторина, а третий - экран результатов. Проблема в том, что я понятия не имею, как подключить кнопку на экране результатов. (есть кнопка, чтобы вернуться к макету меню). Я не знаю, где я могу добавить это:Android, где можно добавить кнопку?
Button haupt = (Button) findViewById(R.id.hauptmeldung);
haupt.setOnclickListener(this); //this refers to the one in the QuizActivty,
abschlusslayout = результат компоновки экрана
activtymain = главная/меню
quizlayout = викторины
Вот мои 3 Java файлы:
MainActivity (QuizActivity):
package at.lorenzdirry.philosophenquiz;
import at.lorenzdirry.philosophenquiz.R.id;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends Activity implements android.view.View.OnClickListener {
Spiellogik spiel;
Button startbut1;
View myView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = (View)findViewById(id.mainlay);
startbut1= (Button)findViewById(id.startb1);
startbut1.setOnClickListener(this);
spiel = new Spiellogik();
}
public View getTheView(){
return myView;
}
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);
else if (id == R.id.startb1)
aufrufen();
else if (id==R.id.hauptmeldung)
Toast.makeText(this, "Leider nichts gewonnen. :-(", Toast.LENGTH_LONG).show();
}
View v;
@Override
public void onBackPressed() {
if(this.findViewById(id.mainlay)==getTheView()){
backButtonHandler();
}
else{
super.onBackPressed();
Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
startActivity(intent);
finish();
}
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
QuizActivity.this);
alertDialog.setTitle("Beenden");
alertDialog.setMessage("Sicher Beenden?");
alertDialog.setPositiveButton("Ja",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.setNegativeButton("Nein",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
public void aufrufen(){
setContentView(R.layout.quiz_layout);
for (int n = 1; n <= 4; n++) {
Button btn = null;
switch (n) {
case 1:
btn = (Button) this.findViewById(R.id.antwort1);
btn.setOnClickListener(this);
break;
case 2:
btn = (Button) this.findViewById(R.id.antwort2);
btn.setOnClickListener(this);
break;
case 3:
btn = (Button) this.findViewById(R.id.antwort3);
btn.setOnClickListener(this);
break;
case 4:
btn = (Button) this.findViewById(R.id.antwort4);
btn.setOnClickListener(this);
break;
}
}
spiel.fragen[spiel.aktFrage].anzeigen(this);
}
}
Frage:
package at.lorenzdirry.philosophenquiz;
import at.lorenzdirry.philosophenquiz.R.id;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
class Frage {
private String frage;
private String frage2;
private String frage3;
private String option1;
private String option2;
private String option3;
private String option4;
private int loesung;
public Frage(String f,String f2,String f3, String o1, String o2, String o3, String o4, int l) {
frage = f;
frage2 = f2;
frage3 = f3;
option1 = o1;
option2 = o2;
option3 = o3;
option4 = o4;
loesung = l;
}
public void anzeigen(Activity quizActivity) {
((TextView) quizActivity.findViewById(id.frage)).setText("\u2022 "+frage);
((TextView) quizActivity.findViewById(id.frage2)).setText("\u2022 "+frage2);
((TextView) quizActivity.findViewById(id.frage3)).setText("\u2022 "+frage3);
((Button) quizActivity.findViewById(id.antwort1)).setText(option1);
((Button) quizActivity.findViewById(id.antwort2)).setText(option2);
((Button) quizActivity.findViewById(id.antwort3)).setText(option3);
((Button) quizActivity.findViewById(id.antwort4)).setText(option4);
}
public boolean richtig(int ausgewaehlt) {
if (ausgewaehlt == this.loesung)
return true;
else
return false;
}
}
Spiellogik:
package at.lorenzdirry.philosophenquiz;
import at.lorenzdirry.philosophenquiz.R.id;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
class Spiellogik{
final int ANZAHL_FRAGEN = 8;
Frage[] fragen = new Frage[ANZAHL_FRAGEN];
Random r = new Random();
int aktFrage = r.nextInt(ANZAHL_FRAGEN);
int gewinnstufe = 0;
Spiellogik() {
fragen[0] = new Frage("624-546 v.Chr.","Monismus","\"Alles besteht aus Wasser.\"",
"Thales von Milet", "Aristoteles", "Platon", "Immanuel Kant", 1);
fragen[1] = new Frage("\u2248600 v.Chr.","Daoismus","\"Wer andere kennt, ist klug. Wer sich selbst kennt, ist weise.\"",
"Laotse ", "Alkmaion", "Roger Bacon", "Sokrates", 1);
fragen[2] = new Frage("570-495 v.Chr.","a\u00B2+b\u00B2=c\u00B2","\"Vernunft ist unsterblich, alles andere sterblich.\"",
"Pythagoras", "Thomas von Aquin", "Archytas von Tarent", "Platon", 1);
fragen[3] = new Frage("563-483 v.Chr.","Buddhismus","\"Gl\u00fccklich ist, wer sein ich \u00fcberwunden hat.\"",
"Siddharta Gautama (Buddha)", "Anaximenes", "Antiphon aus Athen", "Kleanthes", 1);
fragen[4] = new Frage("551-479 v.Chr.","Konfuzianismus","\"Mache Treue und Aufrichtigkeit zu obersten Prinzipien.\"",
"Konfuzius", "Melissos", "Damaskios", "Panaitios", 1);
fragen[5] = new Frage("535-475 v.Chr.","Monismus","\"Alles flie\u00dft. (panta rhei)\"",
"Heraklit", "Pyrrhon", "Proklos", "Theodoros", 1);
fragen[6] = new Frage("490-420 v.Chr.","Relativismus","\"Der Mensch ist Ma\u00df aller Dinge.\"",
"Protagoras", "Xenokrates", "Zenon von Sidon", "Jamblichos", 1);
fragen[7] = new Frage("460-370 v.Chr.","Atomismus","\"In Wirklichkeit gibt es nur die Atome und das Leere.\"",
"Leukipp", "Epicharmos", "Thales von Milet", "Philolaos", 1);
}
void auswerten(int schalter, Activity quizActivity) {
if (!fragen[aktFrage].richtig(schalter)) { // falsch beantwortet
if (gewinnstufe == 0) {
Toast.makeText(quizActivity, "Leider nichts gewonnen. :-(", Toast.LENGTH_LONG).show();
}
else {
String str = "Sie haben Gewinnstufe " + gewinnstufe + " erreicht! :-) - Glckwunsch!!!";
Toast.makeText(quizActivity, str, Toast.LENGTH_LONG).show();
}
// Schalter deaktivieren
((Button) quizActivity.findViewById(id.antwort1)).setEnabled(false);
((Button) quizActivity.findViewById(id.antwort2)).setEnabled(false);
((Button) quizActivity.findViewById(id.antwort3)).setEnabled(false);
((Button) quizActivity.findViewById(id.antwort4)).setEnabled(false);
} else {
aktFrage= r.nextInt(ANZAHL_FRAGEN);
if (gewinnstufe < 7) {
fragen[aktFrage].anzeigen(quizActivity);
gewinnstufe++;
((TextView) quizActivity.findViewById(id.richtigeFragen)).setText("Richtig beantwortete Fragen : "+gewinnstufe+"/8");
((ProgressBar) quizActivity.findViewById(id.progressBar1)).setProgress(gewinnstufe);
}
else {
gewinnstufe++;
((TextView) quizActivity.findViewById(id.richtigeFragen)).setText("Richtig beantwortete Fragen : " + gewinnstufe + "/8");
((ProgressBar) quizActivity.findViewById(id.progressBar1)).setProgress(gewinnstufe);
quizActivity.setContentView(R.layout.abschluss_layout);
}
}
}
}
Я надеюсь, что вы можете помочь мне, пожалуйста :)