首页 > 解决方案 > java.lang.RuntimeException:Android Studio

问题描述

错误如下。当我创建代码并尝试使其工作时,出现以下错误。Libraly 的 fragmentTransaction.replace(R.id.container, fragment) 中的 R.id.container 似乎不可用。我不知道为什么我不能得到 xml<fragment id = container. 尝试过,将 AppCompatActivity 更改为 FragmentActivity/Fragment 等 - 没有修复它。

你能给我一些解决方案吗?

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.animalbook/com.example.animalbook.Libraly}: android.view.InflateException: Binary XML file line #58: Binary XML file line #58: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        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:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #58: Binary XML file line #58: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #58: Error inflating class fragment
     Caused by: java.lang.NullPointerException

package com.example.animalbook

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import kotlinx.android.synthetic.main.activity_libraly.*

class Libraly : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_libraly)

        lionButton.setOnClickListener {
            val fragment = LioneFragment()
            val fragmentManager = this.supportFragmentManager
            val fragmentTransaction = fragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.container, fragment).addToBackStack(null)
        }

        hippoButton.setOnClickListener {
            val fragment = HippoFragment()
            val fragmentManager = this.supportFragmentManager
            val fragmentTransaction = fragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.container, fragment).addToBackStack(null).commit()
        }

        giraffeButton.setOnClickListener {
            val fragment = GiraffeFragment()
            val fragmentManager = this.supportFragmentManager
            val fragmentTransaction = fragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.container,fragment).addToBackStack(null).commit()
        }

        val fragment = titlefragment as? TitleFragment
        fragment?.setTitle("図鑑")
    }
}
<?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=".Libraly">

    <fragment
        android:id="@+id/titlefragment"
        android:name="com.example.animalbook.TitleFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/fragment_title" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_marginBottom="350dp"
        android:orientation="horizontal"
        android:id="@+id/Animalbutoon">

        <Button
            android:id="@+id/lionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lione_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/hippoButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:text="@string/hippo_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/giraffeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/giragge_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </LinearLayout>

    <fragment
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="225dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

标签: javaandroidkotlin

解决方案


推荐阅读