Не удается перейти к фрагменту.Функция ожидает другого типа - PullRequest
1 голос
/ 09 июля 2019

Я пытаюсь создать фрагмент, который охватит половину активности. Я создал фиктивный XML, но не могу перейти на него. Я создал фрагмент через New -> Fragment -> Fragment (Blank). Я не редактировал код созданного фрагмента класса, только XML.

Это мой XML:

<?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="90pt"
android:layout_height="match_parent"
tools:context=".MenuFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Menu"
        android:textSize="45dp"
        android:paddingLeft="10dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLA"
        android:textSize="45dp"
        android:paddingLeft="10dp"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLA"
        android:textSize="45dp"
        android:paddingLeft="10dp"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLA"
        android:textSize="45dp"
        android:paddingLeft="10dp"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLA"
        android:textSize="45dp"
        android:paddingLeft="10dp"

        />



</LinearLayout>

Здесь я хочу перейти к фрагменту. Это нужно сделать нажатием кнопки.

findViewById(R.id.Menu).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(getApplicationContext(), "Menu fragment goes here!", Toast.LENGTH_SHORT).show();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            MenuFragment menuFragment = new MenuFragment();
            fragmentTransaction.replace(android.R.id.content, menuFragment);
            fragmentTransaction.commit();


        }
    });

Это проблема:

Wrong 2nd argument type. Found: 
'com.example.pogolemotoproektce.MenuFragment', required: 
'android.app.Fragment' less... 
Inspection info:
replace
(int,
 android.app.Fragment)
in FragmentTransaction cannot be applied
to
(int,
com.example.pogolemotoproektce.MenuFragment)

происходит в следующей строке:

fragmentTransaction.replace(android.R.id.content, menuFragment);

1 Ответ

4 голосов
/ 09 июля 2019

Вы должны использовать библиотеку поддержки, поскольку это рекомендуется.

Просто замените эту строку:

FragmentManager fragmentManager = getFragmentManager();

на:

FragmentManager fragmentManager = getSupportFragmentManager();

И помните imports вверху (удалите неправильный импорт и замените его правильным, в зависимости от того, используете ли вы androidx или нет, он может отличаться).Вот как должно быть, если вы используете androidx:

import androidx.fragment.app.FragmentManager;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...