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

时间:2015-10-07 09:58:58

标签: android android-fragments

当我尝试在framelayout中加载片段时,应用程序崩溃并在log cat中显示“资源未找到异常”。 当应用程序进行服务调用并尝试多次加载片段时,我收到异常。

我已经搜索过这个问题,但这些帖子或答案都没有帮助我。

Logtrace:

 10-08 05:38:25.866: E/AndroidRuntime(27073): android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7d0
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.content.res.Resources.getResourceName(Resources.java:3192)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:896)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.app.BackStackRecord.run(BackStackRecord.java:833)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.os.Handler.handleCallback(Handler.java:739)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.os.Handler.dispatchMessage(Handler.java:95)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.os.Looper.loop(Looper.java:145)
 10-08 05:38:25.866: E/AndroidRuntime(27073):   at android.app.ActivityThread.main(ActivityThread.java:5832)
10-08 05:38:25.866: E/AndroidRuntime(27073):    at java.lang.reflect.Method.invoke(Native Method)
10-08 05:38:25.866: E/AndroidRuntime(27073):    at java.lang.reflect.Method.invoke(Method.java:372)
10-08 05:38:25.866: E/AndroidRuntime(27073):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
10-08 05:38:25.866: E/AndroidRuntime(27073):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

代码:

 final FrameLayout frame = new FrameLayout(context);
  frame.setId((i+1000)); // since frame layout added at runtime in loop i have added iteration value with 1000 to get unique id.
 final Fragment newFragment = new Fragment()
 mContext.getFragmentManager().beginTransaction().add((i +1000) ,newFragment).commitAllowingStateLoss();

你能帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

这里的问题不在于正在运行的两个不同服务,因此您正在崩溃。

您的例外情况非常清楚地表明ResourceNotFoundException。这意味着不使用给定的id创建framelayout。

所以,请找到可以解决您问题的以下代码:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction;
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);

for(int i=0;i<size();i++){
    FrameLayout layoutMain = new FrameLayout(this);
    layoutMain.setId(Utils.generateViewId());
    layoutMain.setLayoutParams(params);

    fragmentTransaction = fragmentManager.beginTransaction();
    Fragment newFragment = new Fragment()
    fragmentTransaction.add(layoutMain.getId(), newFragment, "HELLO");
    fragmentTransaction.commit();
}

因此,Ids的创建将由Android仅使用17及以上的API版本完成。 下层版本的解决方法如下:

public class Utils{
    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);

    public static int generateViewId() {
        if (Build.VERSION.SDK_INT < 17) {
            for (;;) {
                final int result = sNextGeneratedId.get();
                // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
                int newValue = result + 1;
                if (newValue > 0x00FFFFFF) {
                    newValue = 1; // Roll over to 1, not 0.
        }
                if (sNextGeneratedId.compareAndSet(result, newValue)) {
                    return result;
                }
            }
        } else {
            return View.generateViewId();
        }
    }
}