In my RecyclerView I need replace part of my item to my fragment. I have followed this answer by Victor Cruz and I am able to achieve what I wanted.
Everything is working fine but I am facing one serious problem i.e I am getting Resources$NotFoundException Unable to find resource ID
only in the last item of my RecyclerView, note that this problem is occurring only in the last item rest others are working fine.
Steps which I have tried:
I tried to look for the
resource ID
in R.java file but all in vain.I have tried reducing and increasing the number of items in RecyclerView but the problem is still the same.
- Searched other relevant answers like assigning the same id to parent layout as passing in replace fragment.
- Tried catching the Exception but failed because I think it is not able to find the layout file in on create only in the case of last item.
- Checked if it is happening due to any type casting errors.
Please give suggestions where am I doing wrong. I will be happy to provide any other relevant details.
After 5 days of posting the Question (and struggling with this for a week), I am not able to figure problem out. I have made a small sample app performing this particular task, You can download the code from here.
Please help me out of this.
Edit: Posting code:
private void flipcard(final RecyclerView.ViewHolder holder)
{
final MyHolder myHolderflipcard= (MyHolder) holder;
// Delete old fragment
int containerId = myHolderflipcard.container.getId();// Get container id
Fragment oldFragment = ((FragmentActivity) context).getFragmentManager().findFragmentById(containerId);
if(oldFragment != null)
{
((FragmentActivity) context).getFragmentManager().beginTransaction().remove(oldFragment).commit();
}
int newContainerId = getUniqueId();
// Set the new Id to our know fragment container
myHolderflipcard.container.setId(newContainerId);
// Just for Testing we are going to create a new fragment according
// if the view position is pair one fragment type is created, if not
// a different one is used.
{
Fragment f;
f = new CardBackFragment();
// Then just replace the recycler view fragment as usually
((FragmentActivity) context).getFragmentManager().beginTransaction()
.setCustomAnimations(
R.animator.card_flip_right_in,
R.animator.card_flip_right_out,
R.animator.card_flip_left_in,
R.animator.card_flip_left_out)
.addToBackStack(null)
.replace(newContainerId, f).commit();
myHolderflipcard.cardView.setVisibility(View.GONE);
}
}
// Method that could us an unique id
private int getUniqueId(){
return (int)
SystemClock.currentThreadTimeMillis();
}
Here is my logcat if it can be of any use.
E/UncaughtException: android.content.res.Resources$NotFoundException: Unable to find resource ID #0x1678
at android.content.res.Resources.getResourceName(Resources.java:2209)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: **app package name //intentionally written**, PID: 3136
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x1678
at android.content.res.Resources.getResourceName(Resources.java:2209)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)