简体   繁体   English

android.content.res.Resources $ NotFoundException:无法找到资源ID

[英]android.content.res.Resources$NotFoundException: Unable to find resource ID

In my RecyclerView I need replace part of my item to my fragment. 在我的RecyclerView中,我需要将部分物品替换​​为片段。 I have followed this answer by Victor Cruz and I am able to achieve what I wanted. 我遵循了维克多·克鲁兹(Victor Cruz)的回答,能够实现我想要的。

Everything is working fine but I am facing one serious problem ie 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. 一切工作正常,但是我面临一个严重的问题,即我正在获取Resources$NotFoundException Unable to find resource ID仅在我的RecyclerView的最后一项中Resources$NotFoundException Unable to find resource ID ,请注意,此问题仅在最后一项中发生,其他情况都很好。

Steps which I have tried: 我尝试过的步骤:

  1. I tried to look for the resource ID in R.java file but all in vain. 我试图在R.java文件中查找resource ID ,但徒劳无功。

  2. I have tried reducing and increasing the number of items in RecyclerView but the problem is still the same. 我尝试减少和增加RecyclerView中的项目数量,但问题仍然相同。

  3. Searched other relevant answers like assigning the same id to parent layout as passing in replace fragment. 搜索了其他相关的答案,例如为传递父片段分配相同的ID给父级布局。
  4. 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. 尝试捕获异常,但失败了,因为我认为仅在最后一项的情况下,它无法在create上找到布局文件。
  5. 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. 发布问题5天后 (并为此苦苦了一周), 我无法解决问题。 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. 如果可以用的话,这是我的logcat。

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)

This problem is interesting. 这个问题很有趣。 After debugging your sample app for a while this is my finding: 在调试示例应用程序一段时间后,这是我的发现:

  • The resource id that is missing is the id of created fragment - CardBackFragment . 缺少的resource id是创建的片段的ID- CardBackFragment
  • The resource id is given dynamically after you replace and commit the fragment 替换并提交片段后,将动态给定resource id

For prof of what I have just said, here is a screenshot when I was debugging your app notice the id that is shown: 为了证明我刚才所说的,这里是我调试您的应用程序时的屏幕截图,请注意显示的ID 在此处输入图片说明 And here is the missing id that the debugger told it was missing: 这是调试器告诉它丢失的丢失的ID: 在此处输入图片说明

And to be clear, the newContainerId tis translated into hex id that is here (sorry for my bad english) 需要明确的是, newContainerId翻译为此处的十六进制ID (对不起,我的英语不好)

So what happen here? 那么这里发生了什么?

The answer lies in the way the code is execute the line: myHolderflipcard.cardView.setVisibility(View.GONE); 答案在于代码执行该行的方式: myHolderflipcard.cardView.setVisibility(View.GONE); That was trigger after you commit the fragment to be shown. 提交要显示的片段后,该触发器被触发。

Here is what happen: When you told the card view to be gone, the last item is removed from ui -> Because it is removed from ui and it is the last item on the recycler view -> the height of the recycler view is shorten to minimize the view . 这里是什么情况:当你告诉卡片视图中消失了,最后一个项目是从用户界面中删除- >因为它是从用户界面中删除,这是对回收视图中的最后一个项目- >的高度recycler view是缩短使view最小化。 The bug happen for the last item because the recycler view understand that the row layout that hold the question is empty and it is the last item -> the last item is instead transfer to the question row above. 该错误发生在最后一个项目上,因为recycler view了解到容纳该问题的row layout为空,并且它是最后一个项目->最后一个项目被转移到上面的问题行。 Meanwhile, the thread that insert the fragment into your framelayout is not done. 同时,未将fragment插入framelayout的线程。 So when it's done and it try to find the containerid , it cannot find it. 因此,当它完成并尝试找到containerid ,它找不到它。 Hence, the crash. 因此,崩溃。

So the way to fix it is to wait for the frame to be added completely then you remove the question 因此,解决问题的方法是等待框架完全添加, 然后删除问题

Here is the fix: 解决方法是:

  • Remove myHolderflipcard.cardView.setVisibility(View.GONE); 删除myHolderflipcard.cardView.setVisibility(View.GONE); line from your flipcard method 您的flipcard方法中的一行

  • On the outside create a: private MyHolder curHolder; 在外部创建一个: private MyHolder curHolder;

  • Create a runnable to hide the CardView: 创建一个可运行的以隐藏CardView:

     private Handler handler = new Handler(); private Runnable runnable = new Runnable() { @Override public void run() { Log.d("mId", String.valueOf(curHolder.container.getId())); curHolder.cardView.setVisibility(View.GONE); 

    // handler.postDelayed(this, 500); // handler.postDelayed(this,500); } }; };

  • post it after the commit is done: 提交完成后将其发布:

      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(); // Once all fragment replacement is done we can show the hidden container handler.post(runnable); 

Although it happens really fast. 尽管它发生的非常快。 You can use handler.postDelayed(runnable, 100); 您可以使用handler.postDelayed(runnable, 100); instead if you want to ensure that the fragment is successfully replaced under any circumstances 相反,如果您想确保在任何情况下都能成功替换该片段

And here is the full code (since I'm really bad at english, so I post it just in case) 这是完整的代码(因为我的英语真的很不好,所以我以防万一)

    private void flipcard(final RecyclerView.ViewHolder holder)
    {
        final MyHolder myHolderflipcard= (MyHolder) holder;

        String nim=mysr_id.get(Integer.parseInt(mpref.getradio_button_value()));
        Pattern pattern = Pattern.compile("[0-9]+");
        Matcher matcher = pattern.matcher(nim);
        if (matcher.find())
        {
            currentsrid=Integer.parseInt(matcher.group(0));

            if (currentsrid!=flag)
            {
                flag = Integer.parseInt(matcher.group(0));
                // 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 = 0;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    newContainerId = View.generateViewId();
                }

                // Set the new Id to our know fragment container

                myHolderflipcard.container.setId(newContainerId);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    curHolder = myHolderflipcard;
                }


                // 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();
                    // Once all fragment replacement is done we can show the hidden container
                    handler.post(runnable);

                    //myHolderflipcard.container.setVisibility(View.VISIBLE);
                    //myHolderflipcard.radioGroup.setVisibility(View.GONE);
                    //myHolderflipcard.tvQuestion.setVisibility(View.GONE);
//                    myHolderflipcard.cardView.setVisibility(View.GONE);
                }

            }else
            {
               // backtoorignal=false;
                // ((FragmentActivity)context). getFragmentManager().popBackStack();
            }

        }

    }

    private MyHolder curHolder;
    private Handler handler = new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
           Log.d("mId", String.valueOf(curHolder.container.getId()));
            curHolder.cardView.setVisibility(View.GONE);
        }
    };

Just check small thing. 只是检查小东西。
It might not be the cause but normally it happens when setText on Textview with Integer(number). 它可能不是原因,但通常在具有Integer(number)的Textview上使用setText时会发生。
like bellow 像波纹管

int a = 999;    
myTextView.setText(a);`

so I assume that you use setText call with number inside CardBackFragment 所以我假设您使用SetText调用CardBackFragment内的数字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 奇怪的 android.content.res.Resources$NotFoundException:无法找到资源 ID #0x7f06008d - Weird android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f06008d android.content.res.Resources$NotFoundException · 字符串资源 ID # - android.content.res.Resources$NotFoundException · String resource ID # android.content.res.Resources $ NotFoundException:资源ID#0x0 - android.content.res.Resources$NotFoundException: Resource ID #0x0 缺少资源 ID android.content.res.Resources$NotFoundException:字符串资源 ID #0xb - missing resource id android.content.res.Resources$NotFoundException: String resource ID #0xb java.lang.RuntimeException:无法启动活动android.content.res.Resources $ NotFoundException:资源ID#0x7f110002 - java.lang.RuntimeException: Unable to start activity android.content.res.Resources$NotFoundException: Resource ID #0x7f110002 由 android.content.res.Resources$NotFoundException 引起:即使资源存在,资源 ID 也可绘制(缺少名称) - Caused by android.content.res.Resources$NotFoundException: Drawable (missing name) with resource ID even the resource is there android.content.res.Resources $ NotFoundException:资源ID#0x0 Android错误 - android.content.res.Resources$NotFoundException: Resource ID #0x0 Android error GridView.FATAL例外:main android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 - GridView.FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: String resource ID #0x0 无法访问变量-android.content.res.Resources $ NotFoundException:字符串资源ID - Can't get access to variables - android.content.res.Resources$NotFoundException: String resource ID android.content.res.Resources $ NotFoundException:来自xml类型布局资源ID的文件#0x1020014 - android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM