我有一個負責顯示RecyclerView列表的活動。點擊列表上的項目後,我被重定向到另一個活動以及來自recyclerView適配器的數據。將活動轉換爲片段會停止RecyclerView onclick偵聽器的工作
現在,我打算實現ViewPager tabview,爲此,我必須將活動轉換爲片段。轉換後,新的片段可以顯示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
我要去哪裏錯了?爲什麼片段在活動可能的時候無法從適配器中找到對象?
'fromButton'是什麼和你在哪裏初始化它? –
@PankajKumar它只是一個字符串,爲了導航的目的而進行下一個活動。你認爲它必須做些什麼? – kittu88
在啓動Bundle extras = getActivity()。getIntent()。getExtras(); if(extras == null){ } if(extras!= null){ searchStringResult = extras.getString(「searchString」); fromButton = extras.getString(「Button」); }並嘗試 – Raghavendra