Fragment Dodaj lub zamień nie działa

Używam kodu z tego odniesienia.

Kiedy umieszczam ten kod w moim programie, dostaję błąd, jak widać na poniższym obrazku. Tutaj wpisz opis obrazka

Jakieś powody błędu? The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ExampleFragments)

Kod z mojej głównej działalności:

public void red(View view) {
        android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ExampleFragments fragment = new ExampleFragments();
        fragmentTransaction.replace(R.id.frag, fragment);
        fragmentTransaction.commit();
    }

Przykłady.java

package com.example.learn.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

Tutaj:

package com.example.learn.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
Author: EGHDK, 2012-07-23

4 answers

Problem polega na tym, że mieszasz android.support.v4.app.Fragment i android.app.Fragment. Musisz przekonwertować wszystkie zastosowania, aby korzystać z biblioteki wsparcia, co oznacza również wywołanie getSupportFragmentManager().

Coś takiego, na przykład:

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ExampleFragments fragment = new ExampleFragments();
    fragmentTransaction.replace(R.id.frag, fragment);
    fragmentTransaction.commit();

Ważne jest, aby pamiętać, że biblioteka wsparcia Fragment i normalna Fragment nie są wymienne. Osiągają ten sam cel, ale nie mogą być zastępowane przez siebie w kodzie.

 159
Author: Eric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-23 20:06:00

Chociaż można było odpowiedzieć na to pytanie, należy zauważyć, że rozwiązaniem dla nakładających się fragmentów jest uzyskanie identyfikatora fragmentu (w rzeczywistości identyfikator FrameLayout jako deklarowany w xml będzie powodował bóle głowy) ze świeżą instancją "Fragment":

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new ExampleFragments();
fragmentTransaction.replace(R.id.frag, fragment);
fragmentTransaction.commit();

Nie mogę powiedzieć, ile godzin spędziłem przechodząc przez post za postem bez rozwiązania. Przeczytałem twój drugi post, który jest linkowany w komentarzach powyżej i tam też odpowiem na wszelki wypadek, gdyby ktoś się dowiedział, że najpierw.

Dla tych, którzy otrzymują ClassCastException, Spróbuj również to. Możesz dodać wszystkie odpowiednie biblioteki, używając FragmentActivity zamiast Fragment oraz getactivity ().getSupportFragmentManager w kodzie, aby zatrzymać błędy w ListFragment i nadal napotkasz problemy z fragmentami. Dokumenty Google nie pokazują wszystkiego, a ukończenie kodu Eclipse nie zawsze cię uratuje...czasami musisz sam naprawić błąd!!
 5
Author: whyoz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-17 20:53:33

Bez względu na to, co próbowałem (w tym te napisane tutaj), nie mogłem rozwiązać tego problemu, dopóki nie zobaczyłem tej odpowiedzi

Https://stackoverflow.com/a/5907704/728312

 0
Author: Alpaslan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:24

This worked for me

android.app.FragmentManager fragmentManager = getActivity().getFragmentManager();

 0
Author: Mudasar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-19 13:45:55