I can’t call an Activity through a Fragment

Asked

Viewed 97 times

1

First of all lose by mistakes (keyboard bugle).

Well I’m trying to call an Activity through the code below, this code is in my Mainactivity.java. I created the Fragments to be called through the Drawer Menu and it works perfectly. My goal is to be able to call an Activity directly from this menu but it doesn’t work, the app compiles runs just right but when I touch the desired item (about) the app hangs.

I’m using this code to try to call Activitysobre.

Intent iSobre = new Intent(this, ActivitySobre.class);
startActivity(iSobre);

I have already searched and found codes like getActivity(), however that does not work.

Mainactivity.java

private void displaySelectedScreen(int id){
    android.support.v4.app.Fragment fragment = null;

    switch (id){
        case R.id.nav_home:
            fragment = new FragmentHome();
            break;
        case R.id.nav_notificacao:
            fragment = new FragmentNotificacao();
            break;
        case R.id.nav_favorito:
            fragment = new FragmentFavoritos();
            break;
        case R.id.nav_minhaconta:
            fragment = new FragmentMinhaConta();
            break;
        case R.id.nav_share:
            fragment = new FragmentSobre();
            break;
        case R.id.item_sobre:
            Intent iSobre = new Intent(this, ActivitySobre.class);
            startActivity(iSobre);
            //fragment = new FragmentSobre();
            break;
    }

    if(fragment != null){
        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_main, fragment);
        ft.commit();
    }

Does anyone have any idea how to fix this? Since the Spring.

  • What happens when you do? Intent iSobre = new Intent(getActivity(), Activitysobre.class); getActivity(). startActivity(iSobre);

  • I’m running the app right on my phone. .

  • You can pick up the log in android studio in the RUN tab when it gives problem and see exactly what the problem!

  • Okay, just a moment, I’ll do it.

  • Antonio, I know what the hell happened. I’m about a week trying to make it work and when I went to debug the code it compiled and ran normally. I was able to open Activity.

  • Strange. ?

  • Did you modify something in the code? What could happen is that it needs the Parent context so you would have to pass getActivity(). startActivity(iSobre); . If you didn’t touch anything I wouldn’t know what hahaha was. If that was it, you tell me that I put as an answer.

  • The only thing I changed was that it excludes a line of code from Activitysobre.java And I wrote this code from Intent and gave a Clean and a Rebuild. I tried to use the method informed by Voce but unfortunately it didn’t work.

  • ah blz, but at least it’s working there, right?

  • maybe the mistake was in Activity on and not in the act of opening her

  • Really, maybe that was it. Thank you for your help. Thank you very much.

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.