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?

4

3 回答 3

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;
    }
于 2013-05-08T16:47:42.817 回答
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.

于 2013-05-08T17:12:38.813 回答
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 :)

于 2017-03-08T06:47:41.983 回答