java - 关于 fragment 的类膨胀错误

标签 java android xml android-fragments android-activity

我遇到了有关我的 Fragment 和与之交互的 Activity 的问题:

2020-03-08 19:47:43.864 29471-29471/? I/roid.calculato: Not late-enabling -Xcheck:jni (already on)
2020-03-08 19:47:43.941 29471-29471/? E/roid.calculato: Unknown bits set in runtime_flags: 0x8000
2020-03-08 19:47:43.942 29471-29471/? W/roid.calculato: Unexpected CPU variant for X86 using defaults: x86
2020-03-08 19:47:44.252 29471-29471/fr.android.calculator W/RenderThread: type=1400 audit(0.0:145): avc: denied { write } for name="property_service" dev="tmpfs" ino=23 scontext=u:r:untrusted_app:s0:c133,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=fr.android.calculator
2020-03-08 19:47:44.260 29471-29507/fr.android.calculator D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2020-03-08 19:47:44.261 29471-29507/fr.android.calculator W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2020-03-08 19:47:44.305 29471-29507/fr.android.calculator D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2020-03-08 19:47:44.314 29471-29507/fr.android.calculator D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2020-03-08 19:47:44.323 29471-29507/fr.android.calculator D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2020-03-08 19:47:44.484 29471-29471/fr.android.calculator W/roid.calculato: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-03-08 19:47:44.485 29471-29471/fr.android.calculator W/roid.calculato: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-03-08 19:47:44.563 29471-29471/fr.android.calculator D/AndroidRuntime: Shutting down VM
2020-03-08 19:47:44.566 29471-29471/fr.android.calculator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: fr.android.calculator, PID: 29471
    java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.android.calculator/fr.android.calculator.Activities.CalculatorSecondActivity}: android.view.InflateException: Binary XML file line #8 in fr.android.calculator:layout/activity_calculator_second: Binary XML file line #13 in fr.android.calculator:layout/content_calculator_second: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #8 in fr.android.calculator:layout/activity_calculator_second: Binary XML file line #13 in fr.android.calculator:layout/content_calculator_second: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #13 in fr.android.calculator:layout/content_calculator_second: 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:536)
        at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
        at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3553)
        at android.app.FragmentController.onCreateView(FragmentController.java:104)
        at android.app.Activity.onCreateView(Activity.java:6948)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1263)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1119)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at fr.android.calculator.Activities.CalculatorSecondActivity.onCreate(CalculatorSecondActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
2020-03-08 19:47:44.568 29471-29471/fr.android.calculator E/AndroidRuntime:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-03-08 19:47:44.595 29471-29471/fr.android.calculator I/Process: Sending signal. PID: 29471 SIG: 9

这是我的 Activity :

public class CalculatorSecondActivity extends AppCompatActivity implements LowerCalculatorFragment.OnFragmentInteractionListener {
    private LowerCalculatorFragment lowerCalculatorFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_second);
        lowerCalculatorFragment = LowerCalculatorFragment.newInstance("fragment","you");
        getSupportFragmentManager().beginTransaction().add(R.id.lowerCalculator,lowerCalculatorFragment).commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

这是我的 fragment :

public class LowerCalculatorFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment LowerCalculatorFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static LowerCalculatorFragment newInstance(String param1, String param2) {
        LowerCalculatorFragment fragment = new LowerCalculatorFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    public LowerCalculatorFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_lower_calculator, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

这是我的 Activity 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activities.CalculatorSecondActivity">
    <include layout="@layout/content_calculator_second"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我的 Activity 的内容布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_calculator_second"
        tools:context=".Activities.CalculatorSecondActivity">
    <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
                 tools:ignore="MissingConstraints">
        <fragment android:layout_width="match_parent" android:layout_height="match_parent"
                  android:id="@+id/lowerCalculator"></fragment>
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

最后是与我的 fragment 关联的布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".Fragments.LowerCalculatorFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
            android:background="@drawable/textview_border"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"  android:id="@+id/numberOperators">
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_margin="10dp"
                android:layout_height="match_parent" android:id="@+id/numbers">
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/SevenToPlus">
                <Button
                        android:text="7"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button7" android:layout_weight="1"
                        />
                <Button
                        android:text="8"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button8" android:layout_weight="1"
                        />
                <Button
                        android:text="9"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button9" android:layout_weight="1"
                        />
                <Button
                        android:text="+"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/buttonPlus" android:layout_weight="1"
                        />
            </LinearLayout>
            <LinearLayout

                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" android:id="@+id/FourToMinus-">
                <Button
                        android:text="4"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button4" android:layout_weight="1"
                        />
                <Button
                        android:text="5"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button5" android:layout_weight="1"
                        />
                <Button
                        android:text="6"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button6" android:layout_weight="1"
                        />
                <Button
                        android:text="-"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/buttonMinus" android:layout_weight="1"
                        />
            </LinearLayout>
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/OneToAsterix">
                <Button
                        android:text="1"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button1" android:layout_weight="1"
                        />
                <Button
                        android:text="2"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button2" android:layout_weight="1"
                        />
                <Button
                        android:text="3"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button3" android:layout_weight="1"
                        />
                <Button
                        android:text="*"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/buttonTimes" android:layout_weight="1"
                        />
            </LinearLayout>
            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/ZeroAndDivide">
                <Button
                        android:text="0"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/button0" android:layout_weight="1"
                        />
                <Button
                        android:text="/"
                        android:layout_width="0dp"
                        android:layout_height="match_parent" android:id="@+id/buttonDivide"
                        android:layout_weight="1"
                        />
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
                android:layout_margin="10dp"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent" android:id="@+id/resultButton">
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

你们能看出问题出在哪里吗?因为我不...

预先感谢您的帮助

最佳答案

您正在使用

<fragment android:layout_width="match_parent" android:layout_height="match_parent"
              android:id="@+id/lowerCalculator"></fragment>

<fragment>标签必须android:name 一起使用或class属性指向你的类。由于您不使用其中任何一个,因此在为您的 fragment 查找空类名时会崩溃。

由于您是以编程方式添加 fragment (通过 beginTransaction() ),因此您不应该使用 <fragment>根本没有标签。相反,使用简单的 FrameLayout或者,如果您使用 Fragment 1.2.0或更高版本,您应该使用 FragmentContainerView :

<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
             tools:ignore="MissingConstraints">
    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lowerCalculator" />
</FrameLayout>

关于java - 关于 fragment 的类膨胀错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60591463/

相关文章:

java - UnsatisfiedLinkError:无法加载库

java - 如何动态编辑 HTML 文件的内容

android - 如何使用 Google MAps API v2 围绕引脚绘制圆圈

android - 为什么这些依赖项在 build.gradle 中重新排列? : Android Studio

php - 格式化用 PHP 创建的 XML 文档 - DOMDocument

java - 具有 Web API 的 JWT Introspection 中间件 - Java 和 Identity Server

Java数学库计算日志

android - 如何在android中使用getSharedPreferences

java - 当我尝试使用 maven 和 pom.xml 更改输出目录时出现错误

c# - 使用命名空间查询 XML 的问题