Fragment is unstable in Android Studio


KARUNESH PALEKAR

I am a complete beginner in using Android Studio . Here in this code I am trying to implement a Fragment using a button imposed in the Toolbar .Basically, I want to display the fragment content when clicked on the add button.I want to take few inputs from the user from the fragment update it to Firebase and receive it from Firebase and retrieve it to a recycler view. When I tried to execute the code given below, the fragments blinks and then the app stops working.Any alternative to apply my process would be appreciated.

This is my activity file..



package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;


public class history extends AppCompatActivity {



    enter code here

    EditText success;
    private FrameLayout frameLayout;
    private historyfrag historyfrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);


        Toolbar toolbar = findViewById(R.id.toolbarhistory);
        setSupportActionBar(toolbar);


        ActionBar ab =getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        frameLayout = findViewById(R.id.framelayut);

        historyfrag = new historyfrag();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.toolbarhistorymenu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item){
            switch (item.getItemId()) {

                case R.id.print:
                    Toast.makeText(getApplicationContext(), "Print", Toast.LENGTH_LONG).show();
                    return true;

                case R.id.add:
                    setupfragment(historyfrag);
                    return true;

                default:
                    return super.onOptionsItemSelected(item);


            }



    }

    private void setupfragment(Fragment fragment) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.framelayut,fragment);
        fragmentTransaction.commit();


    }


}

This is the xml file of the activity...

<RelativeLayout 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">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbarhistory"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        android:elevation="6dp"
        app:menu="@menu/toolbarhistorymenu"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="History" />

    <FrameLayout
        android:id="@+id/framelayut"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbarhistory" />

</RelativeLayout>

Found this error in the Logcat

    java.lang.RuntimeException: com.example.myapplication.history@2fb40c4 must implement OnFragmentInteractionListener
        at com.example.myapplication.historyfrag.onAttach(historyfrag.java:85)
        at androidx.fragment.app.Fragment.performAttach(Fragment.java:2574)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:828)
        at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
        at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
        at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        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)

MaCoda

Change your class name to:

public class history extends AppCompatActivity implements YourFragmentClassName.OnFragmentInteractionListener

Inside fragment class you should have:

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

Verwandte Artikel


Fragment is unstable in Android Studio

KARUNESH PALEKAR I am a complete beginner in using Android Studio . Here in this code I am trying to implement a Fragment using a button imposed in the Toolbar .Basically, I want to display the fragment content when clicked on the add button.I want to take few

Fragment auf Android Studio

Kaylee Ich habe zwei Fragmente, aber das zweite Fragment kann beim Klicken nicht geöffnet werden Dies ist das Drogenfragment das zweite Fragment kann sich wegen des Spinners nicht öffnen auf dem ersten Bildschirm platziert und ich weiß nicht, wie ich es behebe

kotlin Android Studio listView in Fragment

Eric Chong Die Listview zeigt im ersten Fragment richtig an, das ist der erste Bildschirm meiner App. aber wenn ich die Listenansicht in ein anderes Fragment einfüge, ist dies nicht der erste Bildschirm der App. Die App wird beendet, wenn ich auf andere Fragme

Android Studio Fragment Inkompatible Typen Fehler

Gosulove import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends App

Fragment und FragmentList zeigen Android Studio nicht an

xcode_Dev Ich möchte eine Registerkartenleiste mit FragmentActivity implementieren. Ich ziele auf API 14 (4.0). Meine App funktioniert einwandfrei, aber die Liste der Registerkartenleisten wird nicht angezeigt. Ich weiß nicht, was ich falsch mache. Vielen Dank

Android Studio-Fragment - onButtonPressed-Methode

Yuli Kima Wenn Sie ein neues Fragment mit Android Studio erstellen, wird die Methode onButtonPressed (Uri) generiert. Wie sollten Sie es in ein UI-Ereignis einbinden, z. B. durch Klicken auf eine in einer XML deklarierte Schaltfläche? Wie soll diese Methode an

Android Studio Map Fragment ist leer

dokelly Ich habe versucht, mithilfe der Google-API ein Kartenfragment auf meinem Startbildschirm zu implementieren. Der Bildschirm wird geladen und es treten keine Fehler auf. Die Karte ist jedoch ein leerer grauer Bildschirm, auf dem nur das Google-Logo angez

Eine Verwirrung über Fragment in Android Studio

rozaimech Dies ist der zweite Tag, an dem ich Android Studio lerne. Ich weiß nicht, warum hier ein Fehler vorliegt. Hilf mir bitte!! //MainActivity.java public void ChangeFragment(View view) { FragmentManager fragmentManager = getFragmentManager();

Android Studio android.support.v4.app.Fragment '

Liad Rosenberg Ich programmiere ein Menü mit Fragmants auf meine App. Ich erhalte die folgende Fehlermeldung: "Falscher 2. Argumenttyp. Gefunden: 'com.example.liadrosenberg.application.MenuActivitys.MyAccountActivity', erforderlich: 'android.support.v4.app.Fra