我已经实现了ricyvlerview搜索,我想听点击,但上下文总是为null,我甚至试图将方法移动到一个活动,我得到了相同的结果
这是我第一次搞乱数据绑定我在这里搜索了很多,但我找不到我做错了什么
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{...}
}
问题
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();
}
我尝试在onAttach中设置上下文并尝试使用
final Context contex = getActivity();
但是在onCategory之外没有任何工作点击上下文不是null但是在onCategory上单击上下文为空
我怎么能克服这个?
修改
如果我没有宣布我的界面是静态的,我会得到这个
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)
答案 0 :(得分:0)
我能够解决这个问题,可能有更好的选择来获得相同的结果,但由于我不是专家,这就是我想出来的。
我在我的片段上创建了一个接口,当调用onCategoryClick时,我调用了我的界面。
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);
}
}