Android fragment, why does an error occur if you don't close the tag?

Asked 2 years ago, Updated 2 years ago, 48 views

Questions about Android Fragment, or whining. That's amazing. Activity code to statically insert both fragments.

<?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_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="example.MainActivity">

    <fragment
        android:id="@+id/roadFragment"
        android:name="example.FragmentOne"
        tools:layout="@layout/fragment_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <fragment
        android:id="@+id/weatherFragment"
        android:name="example.FragmentTwo"
        tools:layout="@layout/fragment_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Isn't this a code that doesn't have a problem at all? There was an Error Inflating class fragment error in the first fragment item, so I did it for several hours. That's all I've got.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentHostCallback.getContext()' on a null object reference

I've done everything to see if it's a content provider or a manifest problem. And I just changed the code to... (without thinking) It's going to work in the world. I have no idea why it's going back. Why is it going back to normal when I close the fragment tag?ㅠ<

<?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_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="example.MainActivity">

    <fragment
        android:id="@+id/roadFragment"
        android:name="example.FragmentOne"
        tools:layout="@layout/fragment_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </fragment>

    <fragment
        android:id="@+id/weatherFragment"
        android:name="example.FragmentTwo"
        tools:layout="@layout/fragment_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </fragment>
</LinearLayout>

android fragment

2022-09-21 21:23

1 Answers

It's the same as closing the tag with </fragment> or </>.

Looking at the error message, there seems to be a problem somewhere else.

The code is working well now, so do </> and run it again. When I turned to the first code you posted, it went back to normal. I don't know if you've fixed the other part. Check it out again~


2022-09-21 21:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.