简体   繁体   中英

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

I had an activity which was responsible for displaying a RecyclerView List. On clicking an item on the list, I was redirected to another activity along with the data from the recyclerView adapter from the same position.

Now, I am tring to implement a ViewPager tabview, and for that, I have to convert the activity to a fragment. After conversion, the new fragment can display the RecyclerView fine, but the onclick stopped working.

The original activity :

Click to see code

The fragment created from the activity :

Click to see code

strong text The onclick listener of RecyclerView:

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());
                        }

                    }
                })
        );

The logcat output in fragment :

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

Where am I going wrong? Why the fragment cannot find the objects from the adapter while the activity can?

Can you please add code like below ?

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

Hope this will help you.

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