您好,Android 重叠背景片段和膨胀异常
Hi, Android overlapping background fragment and inflate exception
我想创建一个 activity 可以在两个不同背景的片段之间切换。我可以用两个按钮在两个片段之间切换。
当我启动 activity 时,我的 Activity 设置如下:
cardEditor
我的片段是绿色的,四个按钮不在片段中。默认情况下,我的片段集是addCardEditor。当我单击按钮 "ajouter" 时,应用程序应该放置 addCardEditor 片段。当我单击按钮 "Voir" 时,我应该会看到我的第二个片段,它被称为 viewCardEditor.
但是当我启动 activity 时,然后当我按下 "Voir" 按钮时,我有 this
在没有任何按钮的情况下,背景仍然是绿色,但我已经将 viewCardEditor 片段的背景设置为灰色!
当我按下 "ajouter" 按钮显示我的 addCardEditor 时,它没有任何改变!我仍然有完全相同的东西。如果我按下任何按钮,它总是保持这种状态!我的两个片段都被这个绿色背景重叠了。
然后当我尝试旋转智能手机时,出现错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jean.cartememoire/com.example.jean.cartememoire.CardEditor}: android.view.InflateException: Binary XML file line #65: Binary XML file line #65: Error inflating class fragment
我在 Whosebug 和其他网站上进行了研究,但没有找到解决我的问题的方法。谢谢你帮助我
我的卡片编辑器 activity 和他的 xml :
package com.example.jean.cartememoire;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import layout.AddCardEditor;
import layout.ViewCardEditor;
public class CardEditor extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_editor);
}
public void switchFragment(View v)
{
Fragment fg;
FragmentManager fm;
FragmentTransaction ft;
if(v == findViewById(R.id.buttonAddCard))
{
fg = new AddCardEditor();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.fragment_card_editor, fg);
ft.commit();
}
if(v == findViewById(R.id.buttonViewCard))
{
fg = new ViewCardEditor();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.fragment_card_editor, fg);
ft.commit();
}
}
}
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:id="@+id/activity_card_editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jean.cartememoire.CardEditor"
android:orientation="vertical"
android:weightSum="1"
android:background="@android:color/black">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="@string/modifier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonModCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
<Button
android:text="@string/supprimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonDeleteCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
<Button
android:text="@string/voir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonViewCard"
android:onClick="switchFragment"
tools:ignore="ButtonStyle" />
<Button
android:text="@string/ajouter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonAddCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
</LinearLayout>
<fragment
android:id="@+id/fragment_card_editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:name="layout.AddCardEditor"
tools:layout="@layout/fragment_view_card_editor" />
</LinearLayout>
然后是我的第一个片段:
package layout;
public class AddCardEditor extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
return inflater.inflate(R.layout.fragment_add_card_editor, container, false);
}
}
和他的 xml :
<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"
tools:context="layout.AddCardEditor"
android:background="@android:color/holo_green_light">
<TextView
android:text="@string/ajouter_une_carte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:elevation="0dp"
tools:ignore="UnusedAttribute"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="129dp"
android:layout_marginTop="16dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="129dp" />
<TextView
android:text="@string/difficult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView11"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="80dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:text="@string/th_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView8"
tools:ignore="UnknownId"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/textView9"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintRight_toRightOf="@+id/textView9" />
<TextView
android:text="@string/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView9"
tools:ignore="MissingConstraints"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="104dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:text="@string/r_ponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView10"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="279dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editTheme"
tools:ignore="MissingConstraints"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView5"
android:layout_marginStart="8dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toRightOf="@+id/textView9"
android:layout_marginLeft="8dp" />
<EditText
android:layout_width="281dp"
android:layout_height="126dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editQuestion"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="@+id/textView9"
app:layout_constraintLeft_toRightOf="@+id/textView8"
android:layout_marginLeft="8dp" />
<EditText
android:layout_width="281dp"
android:layout_height="136dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editReponse"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/editQuestion"
app:layout_constraintLeft_toLeftOf="@+id/editQuestion" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editReponse"
app:layout_constraintLeft_toLeftOf="@+id/editReponse" />
</android.support.constraint.ConstraintLayout>
然后是第二个片段:
package layout;
public class ViewCardEditor extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
return inflater.inflate(R.layout.fragment_view_card_editor, container, false);
}
}
和他的 xml :
<FrameLayout 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="layout.ViewCardEditor"
android:background="@android:color/darker_gray" />
PS : 我把 Hi 放在了我的标题中,因为我无法在我的 post 的开头添加 Hi,太奇怪了...抱歉英语不好
I want to create an activity that can switch between two fragments with different background.
如果 "switch" 你的意思是 adding/removing 片段,那么这些片段中的 none 可以成为你的 XML 布局的一部分 - 通常你可以考虑 XML 基于结构为(某种)只读。因此,在您的项目中,您需要将 <Fragment>
替换为视图组容器,即 <FrameLayout>
,然后在运行时从代码中实例化您的片段并添加到所述容器中。由于这些片段不是 XML 的一部分,您以后可以删除(或替换)它们 w/o 任何问题。
我想创建一个 activity 可以在两个不同背景的片段之间切换。我可以用两个按钮在两个片段之间切换。
当我启动 activity 时,我的 Activity 设置如下: cardEditor
我的片段是绿色的,四个按钮不在片段中。默认情况下,我的片段集是addCardEditor。当我单击按钮 "ajouter" 时,应用程序应该放置 addCardEditor 片段。当我单击按钮 "Voir" 时,我应该会看到我的第二个片段,它被称为 viewCardEditor.
但是当我启动 activity 时,然后当我按下 "Voir" 按钮时,我有 this
在没有任何按钮的情况下,背景仍然是绿色,但我已经将 viewCardEditor 片段的背景设置为灰色!
当我按下 "ajouter" 按钮显示我的 addCardEditor 时,它没有任何改变!我仍然有完全相同的东西。如果我按下任何按钮,它总是保持这种状态!我的两个片段都被这个绿色背景重叠了。
然后当我尝试旋转智能手机时,出现错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jean.cartememoire/com.example.jean.cartememoire.CardEditor}: android.view.InflateException: Binary XML file line #65: Binary XML file line #65: Error inflating class fragment
我在 Whosebug 和其他网站上进行了研究,但没有找到解决我的问题的方法。谢谢你帮助我
我的卡片编辑器 activity 和他的 xml :
package com.example.jean.cartememoire;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import layout.AddCardEditor;
import layout.ViewCardEditor;
public class CardEditor extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_editor);
}
public void switchFragment(View v)
{
Fragment fg;
FragmentManager fm;
FragmentTransaction ft;
if(v == findViewById(R.id.buttonAddCard))
{
fg = new AddCardEditor();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.fragment_card_editor, fg);
ft.commit();
}
if(v == findViewById(R.id.buttonViewCard))
{
fg = new ViewCardEditor();
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.fragment_card_editor, fg);
ft.commit();
}
}
}
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:id="@+id/activity_card_editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jean.cartememoire.CardEditor"
android:orientation="vertical"
android:weightSum="1"
android:background="@android:color/black">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="@string/modifier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonModCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
<Button
android:text="@string/supprimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonDeleteCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
<Button
android:text="@string/voir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonViewCard"
android:onClick="switchFragment"
tools:ignore="ButtonStyle" />
<Button
android:text="@string/ajouter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonAddCard"
android:padding="0dp"
android:paddingBottom="0dp"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:onClick="switchFragment"
tools:ignore="ButtonStyle,RtlSymmetry" />
</LinearLayout>
<fragment
android:id="@+id/fragment_card_editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:name="layout.AddCardEditor"
tools:layout="@layout/fragment_view_card_editor" />
</LinearLayout>
然后是我的第一个片段:
package layout;
public class AddCardEditor extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
return inflater.inflate(R.layout.fragment_add_card_editor, container, false);
}
}
和他的 xml :
<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"
tools:context="layout.AddCardEditor"
android:background="@android:color/holo_green_light">
<TextView
android:text="@string/ajouter_une_carte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:elevation="0dp"
tools:ignore="UnusedAttribute"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="129dp"
android:layout_marginTop="16dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="129dp" />
<TextView
android:text="@string/difficult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView11"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="80dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:text="@string/th_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView8"
tools:ignore="UnknownId"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/textView9"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="32dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintRight_toRightOf="@+id/textView9" />
<TextView
android:text="@string/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView9"
tools:ignore="MissingConstraints"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="104dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:text="@string/r_ponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView10"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="279dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editTheme"
tools:ignore="MissingConstraints"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView5"
android:layout_marginStart="8dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toRightOf="@+id/textView9"
android:layout_marginLeft="8dp" />
<EditText
android:layout_width="281dp"
android:layout_height="126dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editQuestion"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="@+id/textView9"
app:layout_constraintLeft_toRightOf="@+id/textView8"
android:layout_marginLeft="8dp" />
<EditText
android:layout_width="281dp"
android:layout_height="136dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editReponse"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/editQuestion"
app:layout_constraintLeft_toLeftOf="@+id/editQuestion" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editReponse"
app:layout_constraintLeft_toLeftOf="@+id/editReponse" />
</android.support.constraint.ConstraintLayout>
然后是第二个片段:
package layout;
public class ViewCardEditor extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
return inflater.inflate(R.layout.fragment_view_card_editor, container, false);
}
}
和他的 xml :
<FrameLayout 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="layout.ViewCardEditor"
android:background="@android:color/darker_gray" />
PS : 我把 Hi 放在了我的标题中,因为我无法在我的 post 的开头添加 Hi,太奇怪了...抱歉英语不好
I want to create an activity that can switch between two fragments with different background.
如果 "switch" 你的意思是 adding/removing 片段,那么这些片段中的 none 可以成为你的 XML 布局的一部分 - 通常你可以考虑 XML 基于结构为(某种)只读。因此,在您的项目中,您需要将 <Fragment>
替换为视图组容器,即 <FrameLayout>
,然后在运行时从代码中实例化您的片段并添加到所述容器中。由于这些片段不是 XML 的一部分,您以后可以删除(或替换)它们 w/o 任何问题。