0

I have implemented a ricyvlerview search, and i want to listen for clicks, but context is allways null, i even tryed to move the method to an activity and i got the same result

This is my first time messing arround with Data binding i searched alot here but i can't find what i am doing wrong

item_exemple.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

    <variable
        name="model"
        type="pt.condutorresponsavel.android.testescodigo.Search.models.ExampleModel"/>

    <variable
        name="handlers"
        type="pt.condutorresponsavel.android.testescodigo.Study"/>

</data>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:clickable="true">

    <TextView
        android:onClick="@{(v) -> handlers.onCategoryClick(v,model)}"
        android:ellipsize="end"
        android:lines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="@{model.text}"/>

    <View
        android:layout_marginTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/colorPrimary"
        android:alpha="0.17"/>

</FrameLayout>

ExampleViewHolder.java

public class ExampleViewHolder extends SortedListAdapter.ViewHolder<ExampleModel> {

private final ItemExampleBinding mBinding;


public ExampleViewHolder(ItemExampleBinding binding) {
    super(binding.getRoot());
    mBinding = binding;

}



@Override
protected void performBind(ExampleModel item) {
    mBinding.setModel(item);
    mBinding.setHandlers(new Study());

}
}

Study.java

public class Study extends Fragment {
public static Fragment fragment;
public static SearchView searchView;
Context context;



private final String[] pageNames = {"Favorites", ""};

ViewPager pager;

private OnFragmentInteractionListener mListener;

public Study() {  }

public static Estudo newInstance(String param1, String param2) {
    Estudo fragment = new Estudo();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

  ...

    pager = (ViewPager) view.findViewById(R.id.pager);
    pager.setAdapter(buildAdapter());
    return view;
}

private PagerAdapter buildAdapter() {
    return(new SampleAdapter(getActivity(), getChildFragmentManager()));
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;
}

public interface OnFragmentInteractionListener {

    void onFragmentInteraction(Uri uri);
}

public class SampleAdapter extends FragmentPagerAdapter {
    Context ctxt = null;

    public SampleAdapter(Context ctxt, FragmentManager mgr) {
        super(mgr);
        this.ctxt = ctxt;
    }

    @Override
    public int getCount() {
        return (2);
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return Favorites.newInstance(position + 1);
        }else{
            return otherFragment.newInstance(position + 1);
        }
    }

        @Override
        public String getPageTitle ( int position){
            return (String.valueOf(pageNames[position]));
        }
}



public void onCategoryClick(View view, ExampleModel model) {
    Log.d("mTag", "Index: " + model.getId());
    Dialog dialog = new Dialog(getActivity()); //NullPointerException
    dialog.setContentView(R.layout.dialog_pergunta);
    dialog.show();
}


public static class Favorites extends Fragment{...} //RecyclerView is in here

public static class otherFragment extends Fragment{...}
}

Problem

public void onCategoryClick(View view, ExampleModel model) {
Log.d("mTag", "Index: " + model.getId());
Dialog dialog = new Dialog(getActivity()); //NullPointerException
dialog.setContentView(R.layout.dialog);
dialog.show();
}

i tried setting the context in onAttach and also tried to use

final Context contex = getActivity();

but nothing worked outside onCategoryClick the context is not null but inside onCategoryClick the context is null

how can i overcome this?

EDIT

if i don't declare my interface static i get this

NullPointerException: Attempt to invoke interface method 'void pt.condutorresponsavel.android.testescodigo.Study$OnItemClic‌​k.onClick(long)' on a null object reference at
pt.condutorresponsavel.android.testescodigo.Study.onCategory‌​Click(Study.java:303‌​)
Tiago Oliveira
  • 1,427
  • 1
  • 13
  • 29
  • Did you try setting the context in `onCreate()` of your `Fragment`? – Code-Apprentice Sep 01 '16 at 00:51
  • nop, but i just tried your sugesion and the context remains null, i don't have problems using context in other methods but inside onCategoryClick is allways null – Tiago Oliveira Sep 01 '16 at 01:08
  • `onCategoryClick()` is in the `Study` class, right? You should be able to call `getActivity()` directly there because it will be initialized correctly by the time the user is able to click on anything. – Code-Apprentice Sep 01 '16 at 01:12
  • yep `onCategoryClick()` is inside `Study` i followed this great awnser http://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview the only thing i did diferent was adding the `onClick` to the TextView – Tiago Oliveira Sep 01 '16 at 01:17
  • i just noticed that my interface must be static in order to work, if i don't declare my interface static i get a NullpointerException in `onItemClick.onClick(model.getId());` , context is non static so i guess this as something to do with the problem – Tiago Oliveira Sep 01 '16 at 01:58
  • That sounds like you might have other issues. What is the exact error message and what line causes it? – Code-Apprentice Sep 01 '16 at 02:10
  • this is the stack trace `NullPointerException: Attempt to invoke interface method 'void pt.condutorresponsavel.android.testescodigo.Study$OnItemClick.onClick(long)' on a null object reference at pt.condutorresponsavel.android.testescodigo.Study.onCategoryClick(Study.java:303)` this is my first time using Data binding, maybe the issue is on the implementation of the onclick – Tiago Oliveira Sep 01 '16 at 02:29
  • Please edit your question to provide better formatting – Code-Apprentice Sep 01 '16 at 02:30

1 Answers1

0

I was able to fix this, there may be better options to achive the same result but since i am not a expert this was what i come up with.

I created a Interface, on my fragment when the onCategoryClick is called i call a my interface.

static OnItemClick onItemClick;
...
public void onCategoryClick(View view, ExampleModel model) {
    onItemClick.onClick(model.getId());
}


public interface OnItemClick{
    void onClick(long index);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    onItemClick = new OnItemClick() {
        @Override
        public void onClick(long index) {
            Log.d(TAG, "myIndex: " + index);
            Dialog dialog = new Dialog(getActivity());
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.setContentView(R.layout.dialog);
    }
}
Tiago Oliveira
  • 1,427
  • 1
  • 13
  • 29