简体   繁体   中英

Can't see Dialog Fragment in Android Studio

I use dialog fragment to show dialog from activity. There is no error and I can find Log at my Logcat but I can't see the dialog when I run the application. I don't know what's the problem.

Here is my Activity code:

btnEventRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getSupportFragmentManager();

                DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
                Bundle bundle = new Bundle();
                bundle.putString("eventPlaceName", eventPlaceName);
                bundle.putLong("currentUserDistance", currentUserDistance);

                dialogPlusEvent.setArguments(bundle);

                dialogPlusEvent.show(fragmentManager, "DialogPlusEvent");
                Log.d("Dialog Fragment", "Working");
            }
        });

Dialog Fragment code:

public class DialogPlusEvent extends DialogFragment implements View.OnClickListener{

    TextView dpeTvPlaceName, dpeStar, dpeTvMyDistance;
    private String dpePlaceName;
    private Long dpeMyDistance;
    private Button dpeBtnOkay;


    public static DialogPlusEvent getInstance() {
        DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();

        return  dialogPlusEvent;
    }

    @Nullable
    public View OnCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_plus_event, container);

        SharedPreferences sharedPreferences = this.getContext().getSharedPreferences("file", Context.MODE_PRIVATE);
        currentPage = sharedPreferences.getString("currentPage", "");
        
        /.../


        Bundle bundle = getArguments();
        dpePlaceName = bundle.getString("eventPlaceName");
        dpeMyDistance = bundle.getLong("currentUserDistance");


        dpeBtnOkay = view.findViewById(R.id.dpeBtnOkay);
        dpeBtnOkay.setOnClickListener(this);
        setCancelable(false);

        return  view;
    }


    public void onClick(View view) {
        dismiss();
    }
}

You made a mistake while implementing the onCreateView method

Just change the method from OnCreateView to onCreateView like below:

 @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_plus_event, container);

        SharedPreferences sharedPreferences = this.getContext().getSharedPreferences("file", Context.MODE_PRIVATE);
        currentPage = sharedPreferences.getString("currentPage", "");
        
        /.../


        Bundle bundle = getArguments();
        dpePlaceName = bundle.getString("eventPlaceName");
        dpeMyDistance = bundle.getLong("currentUserDistance");


        dpeBtnOkay = view.findViewById(R.id.dpeBtnOkay);
        dpeBtnOkay.setOnClickListener(this);
        setCancelable(false);

        return  view;
    }

@Shay Kin is right. you did not implement the onCreateView method correctly.

I ran the code as you provided, it works well and I can see the dialog, so I think the problem is not in this code.

public class TestFragmentActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_fragment);
        findViewById(R.id.btn_click).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        FragmentManager fragmentManager = getSupportFragmentManager();

        DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();
        Bundle bundle = new Bundle();

        dialogPlusEvent.setArguments(bundle);

        dialogPlusEvent.show(fragmentManager, "DialogPlusEvent");
        Log.d("Dialog Fragment", "Working");
    }
}
public class DialogPlusEvent extends DialogFragment implements View.OnClickListener{

    TextView dpeTvPlaceName, dpeStar, dpeTvMyDistance;
    private String dpePlaceName;
    private Long dpeMyDistance;
    private Button dpeBtnOkay;


    public static DialogPlusEvent getInstance() {
        DialogPlusEvent dialogPlusEvent = new DialogPlusEvent();

        return  dialogPlusEvent;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_plus_event, container);
        setCancelable(false);

        return  view;
    }



    public void onClick(View view) {
        dismiss();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM