![](/img/trans.png)
[英]Hide part of activity_main.xml if keyboard is open (Android)
[英]Android can't open the activity_main.xml without an error
我正在開發測驗應用程序,現在我有兩個XML文件。 一個是開始屏幕的布局,另一個XML文件是測驗的布局。 該測驗實際工作,當我設定的contentview
我quizlayout
,但我顯然希望有startlayout
,在發射升空。 每當我將onCreate
方法中的contentView
設置為startlayout
,我都會方便地關閉該應用程序。
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>
表現:
<?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>
我沒有為測驗發布兩個類文件,因為像我說的那樣,測驗工作正常。
您的問題是,在onCreateMethod中,您引用的按鈕(antwort1,antwort2等)不在開始布局中,而是在測驗布局中。 當然,這會導致錯誤和崩潰。 如果要從開始布局開始,則應放置此部分
// 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);
在另一個功能中,您可以更改為測驗布局。
但是,我認為,最好的辦法是對開始布局使用不同的活動,或者,如果您只希望一個活動,則使用片段(開始布局使用1個片段,測驗布局使用1個片段)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.