[英]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.