0

I was just trying showing a DialogFragment object from an on click inside a button listener.

Here is the code of the activity that should start the Dialog:

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerToButton1();
}

private void addListenerToButton1(){
    final Context context = this;


    button1 = (Button) findViewById(R.id.button1);

    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            DP ciao = new DP();
            ciao.show(this,"MyDP");
        }
    });
}
}

And here's the code of the Dialog:

public class DP extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setMessage("Prova")
    .setPositiveButton("POS", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    })
    .setNegativeButton("NEG", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    });
    return builder.create();
}
}

Errors are:

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

  • The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (new View.OnClickListener(){}, String)

Any advice?

Stefano Munarini
  • 2,711
  • 2
  • 22
  • 26
  • plz show your updated code you can check [here](http://developer.android.com/reference/android/app/DialogFragment.html#show%28android.app.FragmentManager,%20java.lang.String%29) DialogFragment.show also accept FragmentManager instance as first parameter – ρяσѕρєя K May 08 '13 at 16:54
  • Here is the updated code: button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { DP ciao = new DP(); ciao.show(MainActivity.this.getFragmentManager(),"MyDP"); } }); – Stefano Munarini May 08 '13 at 16:56

3 Answers3

2

Show DialogFragment from Activity by passing FragmentManager instance for interacting with fragments associated with current activity as instead of by passing Activity or Button Context:

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {

        DialogFragment ciao = DP.newInstance();
        ciao.show(MainActivity.this.getFragmentManager(),"MyDP");
    }
});

and you will also need to add following newInstance() method in DP DialogFragment which return you DialogFragment instance :

public static DP newInstance() {
        DP frag = new DP();
        return frag;
    }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Doing what you said error is: -The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (FragmentManager, String) – Stefano Munarini May 08 '13 at 16:54
  • ok. did it. Error occours on this line: DialogFragment ciao = DP.newInstance(); Error is: The method newInstance() is undefined for the type DP – Stefano Munarini May 08 '13 at 17:00
  • Always on line that contains DialogFragment ciao = DP.newInstance(); occours an error of convertion: Type mismatch: cannot convert from DP to DialogFragment – Stefano Munarini May 08 '13 at 17:11
1

Try this method of passing the correct context into your DialogFragment

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {

        DP ciao = new DP();
        ciao.show(view.getContext(),"MyDP");
    }
});

In your original code, when calling

'ciao.show(this,"MyDP");'

the this refers to its parent class which is OnClickListener.

When assigning a click listener and overriding the onClick, you get passed the view and an argument which you can use to access information from, including the context.

speedynomads
  • 2,632
  • 1
  • 26
  • 24
  • On the line of the show method error is: view cannot be resolved – Stefano Munarini May 08 '13 at 17:16
  • Did you change onClick(View arg0) to onClick(View view) ? – speedynomads May 08 '13 at 17:18
  • Yes, error now is: The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (Context, String) – Stefano Munarini May 08 '13 at 17:22
  • You can try using 'public class MainActivity extends FragmentActivity' instead of 'public class MainActivity extends Activity' – speedynomads May 08 '13 at 17:26
  • Then call 'FragmentManager fm = getFragmentManager()' before calling the show and pass fm into show as the first argument. 'ciao.show(fm,"MyDP")' – speedynomads May 08 '13 at 17:28
  • If you are using the android support library you would extend from android.support.v4.app.FragmentActivity and call getSupportFragmentManager() instead... – speedynomads May 08 '13 at 17:29
  • Ok got it. But had to do this: android.app.FragmentManager fm = getFragmentManager(); and then passed the fm as first parameter in show. Thank you so much. – Stefano Munarini May 08 '13 at 17:33
  • Here is a good resource for future dialogfragment adventures: http://android-developers.blogspot.co.uk/2012/05/using-dialogfragments.html – speedynomads May 08 '13 at 17:35
1

Extend your fragment (DP) from the android.app.DialogFragment

you can access FragmentManager from within Activity by getFragmentManager()

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {

        DP ciao = new DP();
        ciao.show(getFragmentManager(),"MyDP");
    }
});

Hopefully this works :)

shyam rai
  • 11
  • 4