Ошибка инфляции класса относительно фрагментов - PullRequest
0 голосов
/ 08 марта 2020

Я сталкиваюсь с проблемой, связанной с моим Фрагментом и взаимодействием с ним:

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

Это моя активность:

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) {

    }
}

Это мой фрагмент:

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);
    }
}

Это макет моей Деятельности:

<?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>

И макет содержимого моей Деятельности:

<?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>

И, наконец, макет, связанный с моим Фрагментом:

<?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>

Вы, ребята, потенциально видите, в чем проблема? Потому что я не ...

Заранее спасибо за помощь

1 Ответ

2 голосов
/ 08 марта 2020

Вы используете

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

Тег <fragment> должен использоваться с атрибутом android:name или class, указывающим на ваш класс. Поскольку вы не используете ни один из них, происходит сбой при поиске нулевого имени класса для вашего фрагмента.

Поскольку вы добавляете свой фрагмент программно (через beginTransaction()), вам не следует использовать <fragment> тег на всех. Вместо этого используйте простой FrameLayout или, если вы используете Фрагмент 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...