0

I am trying to call a DialogFragment from my Fragment class. I have an EditText, and would like to call my DialogFragment class in the onClickListener of the EditText I have set up.

I am getting an error in the onClick with the code I have set up trying to call the DialogFragment.

I am getting an error on "show" stating "The method show(FragmentManager, String) in the type DialogFragment is not applicable for the arguments (FragmentManager, String)" and an error on "new Instance" stating "The method newInstance() is undefined for the type MyDialogFragment"

Here's my code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View root = (View) inflater.inflate(R.layout.fragment_profile_fragment, container, false);

        setListenerOnWeight(root);
        button(root);


        return root;
    }

    public void setListenerOnWeight(View v) {
        EditText Weight = (EditText) v.findViewById(R.id.Weight_up);
        Weight.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                weight_frag dialog = new weight_frag.newInstance();
                dialog.show(getFragmentManager(), "fragmentDialog");

            }


        });
    }

for my DialogFragment class:

package com.the.healthescort;


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class weight_frag extends DialogFragment {

    Context mContext;

    public weight_frag() {
        mContext = getActivity();
    }

    public static weight_frag newInstance() {
        weight_frag f = new weight_frag();
        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
        alertDialogBuilder.setTitle("Set Wallpaper?");
        alertDialogBuilder.setMessage("Are you sure?");
        //null should be your on click listener
        alertDialogBuilder.setPositiveButton("OK", null);
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        return alertDialogBuilder.create();
    }




}

1 Answers1

0

i think you should replace

import android.app.Fragment;

with

import android.support.v4.app.Fragment;

in your "my Fragment" class

and you should also provide a method with name newInstance in your dialogfragment class like below :

   public static weight_frag newInstance(){
    weight_frag frag = new weight_frag();
    return frag;
    }

please follow java naming conventions by using capital letter for the starting letter (WeightFragment)

dora
  • 2,047
  • 3
  • 18
  • 20