简体   繁体   中英

Getting null value in fragment spinner

I am trying to use filter in Fragment and implementing the dialog fragment. This is the class that I am using

public class HomeFragment extends Fragment implements FilterDialogFragment.FilterListener, PostAdapter2.OnPostSelectedListener{ detail code }

this the dialogfragment based class for spinner choosing options

public class FilterDialogFragment extends DialogFragment

this method is called upon clicking the filter button, which pops up dialog for spinner options of the filter

Declared

private FilterDialogFragment mFilterDialog;

in onCreateView

mFilterDialog = new FilterDialogFragment();

Method to call

public void  onFilterClicked(){

       mFilterDialog.show(getSupportFragmentManager(), FilterDialogFragment.TAG);

    }

after this upon selecting the spinner option and clicking apply this method is called in which mFilterListener is null which should not be the case

public interface FilterListener {

        void onFilter(Filters filters);

    }



  FilterListener mFilterListener;

public void onSearchClicked() {


        Log.d("Message", String.valueOf(mFilterListener));
        if (mFilterListener != null) {
            Log.d("Message", "New 55555");
            mFilterListener.onFilter(getFilters());
        }

        dismiss();
    }

please assist me to solve this problem. if anymore details are required please let me know

attach method in FilterDialogFragement

 public void onAttach(Context context) {
        super.onAttach(context);

        Log.d("Message", "New 6666666");
        Log.d("Message", String.valueOf(mFilterListener));

        if (context instanceof FilterListener) {

           // Log.d("Message", String.valueOf(mFilterListener));

            mFilterListener = (FilterListener) context;
        }
    }

我假设,你没有在mFilterDialog设置监听mFilterDialog ,所以这就是为什么它为null

You are attempting to mimic this library implementation: Friendly Eats .

However, you do not copy it wholesale, mainly in that you choose to use HomeFragment which implements FilterDialogFragment.FilterListener to launch FilterDialogFragment , rather than the library's MainActivity . This is the cause of your null pointer.

This is due to how getSupportFragmentManager() works. If you look at Android's documentation for this, you will see it says

Return the FragmentManager for interacting with fragments associated with this activity . (My Bolding)

When you call mFilterDialog.show(getSupportFragmentManager(), FilterDialogFragment.TAG); inside HomeFragment , you are actually calling whatever Activity that is the parent of HomeFragment to launch the new FilterDialogFragment . You could double check this by checking if, in onAttach(Context context) inside HomeFragment , if context instanceof HomeFragment . I do not think it will return true .

To solve this, and without changing your use of HomeFragment , you could simply pass an instance of HomeFragment itself, or a separate implementation of FilterDialogFragment.FilterListener (which I'd prefer if you do not need to use anything from HomeFragment other than the listener) to your FilterDialogFragment instance on creation or before you launch it.

For example, you could create a public setter like so:

private FilterListener mFilterListener;

public void setFilterListener(FilterListener filterListener){
    mFilterListener = filterListener;
}

and then in your HomeFragment onCreateView() , you do this:

mFilterDialog = new FilterDialogFragment();

//Or preferably, an anonymous/named implementing instance of the interface only.
mFilterDialog.setFilterListener(this);

Doing so would not rely on the Android framework to provide the initialisation of your field, and does not require you to either change your Activity or HomeFragment you are currently using.

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