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

时间:2016-01-20 06:18:36

标签: java android android-activity fragment

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

现在,我想实现一个ViewPager tabview,为此,我必须将活动转换为片段。转换后,新片段可以显示RecyclerView正常,但onclick停止工作。

原始活动

Click to see code

从活动中创建的片段

Click to see code

强文 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

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

1 个答案:

答案 0 :(得分:2)

您能否添加如下代码?

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

希望这会对你有所帮助。