android.view.InflateException:二进制 XML 文件行 #38:二进制 XML 文件行 #38:膨胀 class 片段时出错

android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment

Android终端机 我在帮助热线的新应用程序的主页中收到错误,我尝试检查所有但仍然没有 运行 请检查并相应评论 请查看

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.googlemaps, PID: 18785
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemaps/com.example.googlemaps.HomeActivity}: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7078)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)
 Caused by: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #38: 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:524)
    at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3551)
    at android.app.FragmentController.onCreateView(FragmentController.java:103)
    at android.app.Activity.onCreateView(Activity.java:6445)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:819)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:902)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:863)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:554)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:461)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:383)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.example.googlemaps.HomeActivity.onCreate(HomeActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:7327)
    at android.app.Activity.performCreate(Activity.java:7318)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7078)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)

xml 文件

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 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="HomeActivity">


        <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="125dp"
        android:layout_marginTop="5dp"
        android:text="Welcome! You are logged in"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText"
        android:layout_marginStart="125dp" />
        <TextView
            android:id="@+id/texttitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="125dp"
            android:layout_marginTop="25dp"
            android:text="24x7 SECURITY HELPLINE"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="HardcodedText"
            android:layout_marginStart="50dp" />

        <fragment
            android:id="@+id/home"
            android:layout_height="match_parent"
            android:layout_width="140dp"
            android:layout_marginTop="80dp"
            tools:context=".HomeActivity"
            android:background="#FA8072" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="140dp"
            android:layout_height="130dp"
            android:layout_marginTop="80dp"
            tools:src="@tools:sample/avatars"
            tools:ignore="HardcodedText"
            android:contentDescription="Profile Pic"/>

        <Button
            android:id="@+id/profile"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="210dp"
            android:text="Your Profile"
            tools:ignore="HardcodedText"/>
        <Button
            android:id="@+id/complain"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="250dp"
            android:text="Complain"
            tools:ignore="HardcodedText"/>
        <Button
            android:id="@+id/tips"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="290dp"
            android:text="Safety Tips"
            tools:ignore="HardcodedText"/>
        <Button
            android:id="@+id/alerts"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="330dp"
            android:text="Alerts"
            tools:ignore="HardcodedText"/>
        <Button
        android:id="@+id/emergency"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="370dp"
        android:text="emergency"
        tools:ignore="HardcodedText"/>
        <Button
            android:id="@+id/nearby"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="410dp"
            android:text="NearBy"
            tools:ignore="HardcodedText"/>
        <Button
            android:id="@+id/volunteer"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="450dp"
            android:text="Volunteer"
            tools:ignore="HardcodedText"/>


        <Button
            android:id="@+id/logout"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="1dp"
            android:layout_marginTop="490dp"
            android:text="Logout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2"
            tools:ignore="HardcodedText"
            android:layout_marginStart="1dp" />

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="170dp"
            android:layout_marginLeft="170dp"
            android:layout_marginTop="400dp"
            android:shadowRadius="3"
            android:textColor="#0000FF"
            android:text="Location sharing"
            tools:ignore="HardcodedText" />
        <Button
            android:id="@+id/custom_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:layout_marginLeft="150dp"
            android:background="@drawable/custom_button"
            android:layout_marginStart="150dp" />

        <Switch
            android:id="@+id/switch_enable_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:layout_marginLeft="200dp"
            android:layout_marginTop="350dp"
            android:text="Panic Alert"
            android:layout_marginStart="200dp"
            tools:ignore="HardcodedText"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="225dp"
            android:layout_marginLeft="210dp"
            android:text="Panic Mode"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="20sp"
            tools:ignore="HardcodedText"
            android:layout_marginStart="200dp" />
        <Button
            android:id="@+id/report"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="480dp"
            android:layout_marginLeft="170dp"
            android:text="Report"
            android:textColor="#FF0000"
            tools:ignore="HardcodedText"
            android:layout_marginStart="180dp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="130dp"
            android:layout_marginLeft="150dp"
            tools:ignore="HardcodedText"
            android:text="Create your panic alert button "
            android:layout_marginStart="150dp" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="650dp"
            android:layout_marginLeft="150dp"
            tools:ignore="HardcodedText"
            android:text="Feedback"
            android:textColor="#FFA500"
            android:layout_marginStart="150dp" />
        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="650dp"
            android:layout_marginLeft="280dp"
            tools:ignore="HardcodedText"
            android:text="Write to us"
            android:textColor="#FFA500"
            android:layout_marginStart="280dp" />

</FrameLayout>

java 文件

package com.example.googlemaps;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.auth.FirebaseAuth;
public class HomeActivity extends AppCompatActivity {
    Button btnLogout;
    Button customButton;
    Switch switchEnableButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchactivity();
            }
        });
        btnLogout = findViewById(R.id.logout);
        customButton = findViewById(R.id.custom_button);
        switchEnableButton = findViewById(R.id.switch_enable_button);
        customButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HomeActivity.this, "Click", Toast.LENGTH_SHORT).show();
            }
        });

        switchEnableButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    customButton.setEnabled(true);
                } else {
                    customButton.setEnabled(false);
                }
            }
        });
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirebaseAuth.getInstance().signOut();
                Intent intToMain = new Intent(HomeActivity.this, LoginActivity.class);
                startActivity(intToMain);
            }
            });
            }
    public void launchactivity(){
        Intent intent = new Intent( HomeActivity.this, MapsActivity.class );
        startActivity( intent );
    }
}

我的帮助热线新应用程序的主页出现错误,我尝试检查所有但仍然没有运行请检查并相应评论 请评论

您忘记了片段中的 android:name="" 属性。如果您想动态添加片段,请使用 ViewGroup(FrameLayout 等)

您在 xml 中定义片段的代码不正确。请阅读:https://developer.android.com/training/basics/fragments/creating。 希望对您有所帮助。