0

I have the main class and more 2 fragment classes but one of these is extended from FragmentActivity, when I call a fragment from a class which isn't extended from FragmentActivity but just extended from Fragment, it works. How could I call a fragment extended from FragmentActivity in main class which is extended from appCompacAtivity?

Here is my code from main activity (Principal_eng.Java):

import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
public class Principal_eng extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.principal_eng);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /*
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        */
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        FragmentManager fr = getFragmentManager();
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.ceramicas) {
            fr.beginTransaction()
                    .replace(R.id.content_main, new Frag1() )//Here i get an error
                    .commit();
        } else if (id == R.id.tijolos) {
            fr.beginTransaction()
                    .replace(R.id.content_main, new Frag2())
                    .commit();
        } else if (id == R.id.ohms) {

        } else if (id == R.id.nav_send) {

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Below is the code from fragment one (Frag1.Java)

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

public class Frag1 extends FragmentActivity {
    public View onCreateView(LayoutInflater inf, ViewGroup cont, Bundle savedInst) {
        View v = inf.inflate(R.layout.frag1, cont, false);
        return v;
    }
}

Code from fragment two (Frag2.Java)

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

public class Frag2 extends Fragment{
    public View onCreateView(LayoutInflater inf,ViewGroup cont, Bundle savedInst){
        View v = inf.inflate(R.layout.frag2,cont,false);
        return v;
    }
}
sparkhee93
  • 1,381
  • 3
  • 21
  • 30
Fantoche
  • 24
  • 4

3 Answers3

1

Frag1 extends FragmentActivity and that is not a Fragment; it's declared as an Activity.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
1

A FragmentActivity is an Activity, not a Fragment so what you are trying to do doesn't make sense: trying to perform a fragment transaction on an Activity.

Why not just subclass Fragment as you have done with Frag2?

Kent Hawkings
  • 2,793
  • 2
  • 25
  • 30
  • Because i want to call another fragment from Frag1, and i have seen that it's need to be a fragmentactivity to implements listeners on buttons – Fantoche Feb 01 '16 at 18:25
0

Your problem is that your Frag2 class is an activity and not a Fragment. You should make it extends Fragment instead of an activity. You cannot put an activity inside a fragment container.

If you want to call your activity from your mainActivity class do from your main activity:

Intent intent = new Intent(this, Frag1.class);
startActivity(intent);

Good luck!

Howy
  • 101
  • 7