繁体   English   中英

将活动转换为片段会阻止RecyclerView onclick侦听器正常工作

[英]Converting activity into fragment is stops the RecyclerView onclick listener from working

我有一个活动,负责显示RecyclerView列表。 单击列表上的项目后,我从同一位置被重定向到另一个活动以及来自recyclerView适配器的数据。

现在,我打算实现ViewPager选项卡视图,为此,我必须将活动转换为片段。 转换后,新片段可以正常显示RecyclerView,但onclick停止工作。

原始活动

点击查看代码

从活动创建的片段

点击查看代码

强文本 RecyclerView的onclick侦听器:

recyclerView.addOnItemTouchListener(
                new com.studystory.utilities.RecyclerItemClickListener(getActivity(), new com.studystory.utilities.RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        Log.e("position", ""+position);
                        try {
                            final Student s = (Student) mAdapter.getObjectAt(position);
                            final Intent i = new Intent(getActivity().getApplicationContext(), ViewStory.class);
                            final int pos = position;
                            if (fromButton.equalsIgnoreCase("browseStoriesButton")) {
                                i.putExtra("Button", "browseStoriesButton");
                            } else {
                                i.putExtra("Button", "notbrowseStoriesButton");
                            }

                            String dateOfBirthStr = "";

                            try {
                                dateOfBirthStr = TimeSplitterController.generateAge(s.getDateOfBirth().toString());
                                i.putExtra("dateOfBirthStr", dateOfBirthStr);
                            } catch (java.text.ParseException e) {
                                e.printStackTrace();
                            }
                            final String dateOfBirthString = dateOfBirthStr;

                            if (s.getByteArray() == null) {
                               /* try {
                                    //We assume they have no idea associated.
                                    Bitmap tempBitmap = null;
                                    tempBitmap = ImageController.resizeToHighResolutionCircle(tempBitmap, getApplicationContext());
                                    String bitmapStr = ImageController.bitmapToStringOld(tempBitmap, getApplicationContext());
                                    i.putExtra("bitmapStr", bitmapStr);
                                    tempBitmap.recycle();
                                    tempBitmap = null;
                                } catch (Exception e) {
                                    Log.e("Exception",e.toString());
                                }*/
                            } else {
                                //They have an image.
                                Bitmap tempBitmap = ImageController.BitmapCompress(s.getByteArray());
                                if (tempBitmap != null) {
                                    tempBitmap = ImageController.resizeToHighResolutionCircle(tempBitmap, getActivity().getApplicationContext());
                                } else {
                                    tempBitmap = ImageController.resizeToCircle(tempBitmap, getActivity().getApplicationContext());
                                }
                                String bitmapStr = ImageController.bitmapToStringOld(tempBitmap, getActivity().getApplicationContext());
                                i.putExtra("bitmapStr", bitmapStr);
                                tempBitmap.recycle();
                                tempBitmap = null;
                            }


                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {


                                    i.putExtra("studentObject", s);
                                    startActivity(i);

                                }
                            }, 50);

                            Log.e("Clicked", "" + position);
                        }
                        catch (Exception e){
                            Log.e("List issue", e.toString());
                        }

                    }
                })
        );

片段中的logcat输出

List issue: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference

我要去哪里错了? 为什么片段在活动可以的同时就无法从适配器找到对象?

您能添加下面的代码吗?

if(fromButton!=null){
 if (fromButton.equalsIgnoreCase("browseStoriesButton")) {
        i.putExtra("Button", "browseStoriesButton");
  } else {
        i.putExtra("Button", "notbrowseStoriesButton");
  }
 }else{
  i.putExtra("Button", "notbrowseStoriesButton");
}

希望这会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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