-1

I am trying to use a floating action button in a fragment to bring up a dialog to create a new post. The issue I am having is that I get an error on this line of code:

popAddPost = new Dialog(this);

The error states that:

Dialog (android.content.context) in Dialog cannot be applied to (com.comhar.firebaseapp.Fragments.ForumFragment)

I have tried using a few solutions i found online but none have worked

ForumFragment.java

public class ForumFragment extends Fragment {

    Dialog popAddPost;


    public ForumFragment() {
        // Required empty public constructor
    }


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

        iniPopup();

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popAddPost.show();
            }
        });

        return view;
    }

    private void iniPopup() {

        popAddPost = new Dialog(this);
        popAddPost.setContentView(R.layout.popup_add_post);
        popAddPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popAddPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.WRAP_CONTENT);
        popAddPost.getWindow().getAttributes().gravity = Gravity.TOP;


    }

}

fragment_forum.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.ForumFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="forum" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="20dp"
        android:src="@drawable/nep_post" />

</FrameLayout>

The app will not run as I am recieving the error:

error: incompatible types: ForumFragment cannot be converted to Context

any advice would be greatly appreciated!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
cian404
  • 21
  • 3
  • hello @cian404, did you tried my answer below? did it helped you to solve your problem? if it did, please consider to mark my answer as accepted by clicking the arrow in left. so that, others can also benefited from the solution. – sourav.bh Mar 24 '19 at 16:35

2 Answers2

0

Update your iniPopup() method like this:

private void iniPopup() {
    popAddPost = new Dialog(getActivity());
    popAddPost.setContentView(R.layout.activity_login);
    popAddPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    popAddPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.WRAP_CONTENT);
    popAddPost.getWindow().getAttributes().gravity = Gravity.TOP;
}

Let me know in case of any query.

sourav.bh
  • 467
  • 1
  • 7
  • 20
0

You should init the dialog with an proper context, it can be the context where the Fragment resides or could be the activity which holds the Fragment. So, replace this line:

popAddPost = new Dialog(this);

by this:

popAddPost = new Dialog(getActivity());
sourav.bh
  • 467
  • 1
  • 7
  • 20