Android 如何在活动中成功地将内容显示为RunningFragment类中的片段?
我对编码是新手,这是我第一次使用片段。我试图显示从片段到活动的内容。我命名了这个片段(我发现这是必要的),并在我正在使用的活动(activity_running)中找到了一个代码,但我得到了一个错误,上面写着:“错误膨胀类android.widget.RelativeLayout”。我想可能是布局有问题。我将向你们展示我到目前为止所做的一切,我希望有人能帮助我。我接受任何建议。如果我的代码有什么问题,你告诉我一种方法,使其工作,将不胜感激 我当前的错误是:Android 如何在活动中成功地将内容显示为RunningFragment类中的片段?,android,android-layout,android-fragments,fragment,Android,Android Layout,Android Fragments,Fragment,我对编码是新手,这是我第一次使用片段。我试图显示从片段到活动的内容。我命名了这个片段(我发现这是必要的),并在我正在使用的活动(activity_running)中找到了一个代码,但我得到了一个错误,上面写着:“错误膨胀类android.widget.RelativeLayout”。我想可能是布局有问题。我将向你们展示我到目前为止所做的一切,我希望有人能帮助我。我接受任何建议。如果我的代码有什么问题,你告诉我一种方法,使其工作,将不胜感激 我当前的错误是: Error inflating cla
Error inflating class android.widget.RelativeLayout
Caused by: android.view.InflateException: Binary XML file line #2 in com.example.diligent:layout/activity_running: Binary XML file line #100 in com.example.diligent:layout/fragment_running: Binary XML file line #100 in com.example.diligent:layout/fragment_running: Error inflating class android.widget.RelativeLayout
Caused by: android.view.InflateException: Binary XML file line #100 in com.example.diligent:layout/fragment_running: Binary XML file line #100 in com.example.diligent:layout/fragment_running: Error inflating class android.widget.RelativeLayout
Caused by: android.view.InflateException: Binary XML file line #100 in com.example.diligent:layout/fragment_running: Error inflating class android.widget.RelativeLayout
Caused by: java.lang.reflect.InvocationTargetException
位于android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
下面是我现在正在使用的图片:
跑步活动:
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import com.example.diligent.R;
public class Running extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_running);
//Content displayed as Fragment from RunningFragment class.
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(R.id.fragment, RunningFragment.class, null)
.commit();
}
}
}
布局:
<androidx.fragment.app.FragmentContainerView
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:name="com.example.diligent.running.RunningFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_running"
android:orientation="vertical"
tools:context=".running.Running">
</androidx.fragment.app.FragmentContainerView>
。
.
.
错误在这一行:
View view = inflater.inflate(R.layout.fragment_running, container, false);
正在运行的程序段:
<LinearLayout 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:orientation="vertical"
android:id="@+id/fragment_run"
tools:context=".running.RunningFragment">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="1"
android:columnCount="1"
>
<androidx.cardview.widget.CardView
android:id="@+id/Plate"
android:layout_gravity="fill"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="@color/run"
android:layout_height="60dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Running"
android:textColor="@color/white"
android:textSize="30dp"
android:paddingLeft="140dp"
android:paddingTop="10dp">
</TextView>
<ImageView
android:id="@+id/backRun"
android:layout_width="50dp"
android:layout_height="45dp"
android:src="@drawable/white_arrow_back_24"
android:paddingTop="15sp"
android:paddingLeft="10sp"
app:tint="@color/white" />
</androidx.cardview.widget.CardView>
</GridLayout>
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="300dp"
app:cardElevation="6dp"
app:cardUseCompatPadding="true"
android:innerRadius="0dp"
android:shape="ring"
app:cardCornerRadius="110dp"
app:cardBackgroundColor="@color/white"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@drawable/shape_circle"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/run"
app:tint="@color/blue" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="30dp"
android:textColor="@color/blue"
android:id="@+id/stepText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray2"
android:text="Steps Count" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<RelativeLayout
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">
<TextView
android:id="@+id/stepLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="@string/steps_label"
android:textSize="20sp" />
<TextView
android:id="@+id/timeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp"
tools:text="Time: "
android:textSize="20sp" />
<TextView
android:id="@+id/distanceText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/timeText"
android:layout_marginTop="10dp"
tools:text="Distance: "
android:textSize="20sp" />
<TextView
android:id="@+id/orientationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/distanceText"
android:layout_marginTop="10dp"
tools:text="Orientation: "
android:textSize="20sp" />
<TextView
android:id="@+id/speedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/orientationText"
android:layout_marginTop="10dp"
tools:text="Average speed: "
android:textSize="20sp" />
<Button
android:id="@+id/settingsButton"
style="?android:attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#1b6f97"
android:text="@string/settings"
android:textAllCaps="false"
android:textColor="#f9f9f7" />
<Button
android:id="@+id/startButton"
style="?android:attr/selectableItemBackground"
android:layout_width="100dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ded9d9"
android:text="@string/start"
android:textColor="@color/remain_white" />
<Button
android:id="@+id/resetButton"
style="?android:attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/settingsButton"
android:background="#1b6f97"
android:text="@string/reset"
android:textAllCaps="false"
android:textColor="#f9f9f7" />
<TextView
android:id="@+id/achievedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/achieved"
android:textColor="#116892"
android:textSize="24sp"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
如果你还想让我给你看什么,告诉我。提前谢谢你 请检查您的导入部分,因为Fragment可以从android.app.Fragment或android.support.v4.app.Fragment扩展,请检查从活动导入并运行Fragment我看到的一个问题是,您正在声明活动布局以使用
标记,然后在运行时创建另一个Fragment
您的布局文件:
<fragment 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:name="com.example.diligent.running.RunningFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_running"
android:orientation="vertical"
tools:context=".running.Running">
</fragment>
调用setContentView(R.layout.activity\u running)时代码>,由于布局中的
标记,已将片段添加到活动中。然后尝试用一个新的RunningFragment
实例替换它,并使用ft.replace(R.id.fragment,new RunningFragment())
,但replace
用于布局容器,而不是
。这可能是你的问题
使用
添加后来发现有问题的片段是一种过时的方法。请查看使用FragmentContainer
的推荐方法,看看这是否解决了您的问题。RuunnifFragment扩展了Fragment?或者显示运行片段classIt扩展片段的代码。如果你觉得有必要,我会显示代码。请共享代码。我刚做了。导入android.app.Fragment;在activty和fragment类中是一样的吗?你有什么建议?更改导入?密码?用什么?androidx.fragment.app.fragment在你的fragment类中添加此导入并删除导入android.app.fragment谢谢你的建议。它修复了代码中的错误。但现在我有一个新的错误。它说:错误膨胀类android.widget.RelativeLayout。此处:视图=充气机。充气(R.layout.fragment\u running,container,false);代码在我的问题中。
<fragment 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:name="com.example.diligent.running.RunningFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_running"
android:orientation="vertical"
tools:context=".running.Running">
</fragment>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_running); // This adds a fragment from the layout file
findViewById(R.id.backRun).setOnClickListener(v -> onBackPressed());
//Content displayed as Fragment from RunningFragment class.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment, new RunningFragment()); // This tries to overwrite the first fragment incorrectly
ft.commit();
}