Java 在主活动中膨胀片段会导致错误

Java 在主活动中膨胀片段会导致错误,java,android,android-fragments,Java,Android,Android Fragments,我希望能够在我的主活动类中的两个片段之间进行交换。但是当膨胀片段时,我的应用程序崩溃了。下面是我的代码: 这是将放置我的片段的XML代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"

我希望能够在我的主活动类中的两个片段之间进行交换。但是当膨胀片段时,我的应用程序崩溃了。下面是我的代码:

这是将放置我的片段的XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="@drawable/rounded_dialog"
    android:orientation="vertical"
    app:behavior_hideable="false"
    app:behavior_peekHeight="56dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">


    <fragment
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"/>


</LinearLayout>
下面是我的主要活动中的代码,用于放大布局

    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(findViewById(R.id.fragment_place)!=null){


            if (savedInstanceState != null) {
                return;
            }

            SearchLocation fragment = new SearchLocation();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_place, fragment).commit();

        }
我的logcat中出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.swim, PID: 20462
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.swim/com.example.user.swim.MainActivity}: android.view.InflateException: Binary XML file line #29: Binary XML file line #14: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6540)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: android.view.InflateException: Binary XML file line #29: Binary XML file line #14: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
 Caused by: java.lang.NullPointerException
    at java.lang.VMClassLoader.findLoadedClass(Native Method)
    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.Fragment.instantiate(Fragment.java:617)
    at android.app.FragmentContainer.instantiate(FragmentContainer.java:49)
    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3598)
    at android.app.FragmentController.onCreateView(FragmentController.java:98)
    at android.app.Activity.onCreateView(Activity.java:6182)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:862)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.example.user.swim.MainActivity.onCreate(MainActivity.java:87)
    at android.app.Activity.performCreate(Activity.java:6980)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6540)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是片段搜索位置的XML

    <?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:layout_width="match_parent"

        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_margin="10dp"
        android:text="@string/where_would_you_like_to_go"
        android:textAlignment="center" />



    <EditText
        android:id="@+id/destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:padding="10dp"
        android:drawablePadding="5dp"
        android:drawableStart="@drawable/ic_search_black_24dp"
        android:drawableLeft="@drawable/ic_search_black_24dp"
        android:textSize="18sp"
        android:inputType="text"
        android:background="@drawable/rectangle"
        android:hint="Search for destination"
        android:maxLines="1"
        android:imeOptions="actionDone"

        android:textAlignment="textStart" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/search_destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/search_destination" />



</LinearLayout>

我不想在XML中为片段添加name/class属性,因为当您查看官方文档时,添加这些属性将无法在运行时更改片段


此外,我还尝试更改MainActivity类以扩展ActivityFragment,但经过进一步研究,我发现ActivityFragment是AppCompatActivity的一个子类。

您可以尝试使用
Framelayout
而不是
fragment
,如下所示

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" />

我复制了您的代码并尝试使用它,当我在SearchLocation中使用fragment时,它会出错,因此我将其更改为Framlayout。 无法真正判断代码中的错误,但在我做了更改后,这一点起了作用 主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.fragment_place) != null) {


            if (savedInstanceState != null) {
                return;
            }
            SearchLocation fragment = new SearchLocation();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_place, fragment).commit();
        }
    }
SearchLocation java


import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;



public class SearchLocation extends Fragment {

    private EditText mSearch;
    private Button mDestination;
    private RecyclerView mRecyclerView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

         View view = inflater.inflate(R.layout.fragment_search_location, null);

        mSearch = view.findViewById(R.id.search_destination);
        mDestination = view.findViewById(R.id.destination);
        mSearch.setText("Hello world");
        RecyclerView mRecyclerView = view.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        /*searchLocation(view);*/

        return view;

    }

搜索位置xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".SearchLocation">

    <EditText
        android:id="@+id/search_destination"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/destination"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_destination" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/destination" />
</androidx.constraintlayout.widget.ConstraintLayout>


我尝试了FrameLayout,但仍然存在相同的错误,为什么我要使用replace,但没有以前的片段我正在ReplacingBase这里您的
片段搜索位置
xml我已经为片段搜索位置添加了xml。当我使用name属性创建片段时,它会显示出来,但是如果我这样做了,我就无法在运行时更改片段。实际上,您的解决方案可以工作,但我不知道为什么它可以工作。很高兴我提供了帮助。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".SearchLocation">

    <EditText
        android:id="@+id/search_destination"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/destination"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_destination" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/destination" />
</androidx.constraintlayout.widget.ConstraintLayout>