Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting an error "The method show(FragmentManager, String)"

Tags:

android

I am getting an error

The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (FragmentManager, String)

package com.example.test1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        DialogFragment newFragment = new FireMissilesDialogFragment();
        newFragment.show(getFragmentManager(), "missiles");
    }

    public boolean onCreateOtionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
like image 343
Vinuthan Avatar asked Nov 01 '12 11:11

Vinuthan


3 Answers

As you're using android.support.v4.app.DialogFragment, you should pass to show() an instance of android.support.v4.app.FragmentManager which can be queried using an getSupportFragmentManager() call. Hope this helps.

like image 129
Egor Avatar answered Oct 23 '22 18:10

Egor


The problem is because you need to be using the support package's FragmentManager but you are using the native FragmentManager when you call getFragmentManager(). Try calling getSupportFragmentManager() when initializing your variable fm

you also have to make sure that you include DialogFragment from the Support package and not from the native package.

You can do that by importing,

import android.support.v4.app.DialogFragment;
like image 43
Devangi Desai Avatar answered Oct 23 '22 19:10

Devangi Desai


Even i had the same problem when running the code in gingerbread. But works fine for ICS. The solution is,

instead of this:

public class MainActivity extends Activity {
}

use extends FragmentActivty

public class MainActivity extends FragmentActivity {
}
like image 19
suresh cheemalamudi Avatar answered Oct 23 '22 20:10

suresh cheemalamudi