android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
Asked Answered
B

12

77

I hate posting code, and asking why it doesn't work, but I'm at a loss right now. I've tried to run the following class below, and I get an exception in logcat which I can't explain. Even a google search yields little results. Maybe I just don't understand the ecosystem, but the exception has little information. The exception I'm getting is below:

03-06 21:50:43.031: E/AndroidRuntime(1013): FATAL EXCEPTION: main
03-06 21:50:43.031: E/AndroidRuntime(1013): android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.content.res.Resources.getResourceName(Resources.java:1659)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.View.measure(View.java:15513)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.View.measure(View.java:15513)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.View.measure(View.java:15513)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.View.measure(View.java:15513)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.os.Handler.handleCallback(Handler.java:725)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.os.Looper.loop(Looper.java:137)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at java.lang.reflect.Method.invoke(Method.java:511)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-06 21:50:43.031: E/AndroidRuntime(1013):     at dalvik.system.NativeStart.main(Native Method)
03-06 21:55:44.231: E/Trace(1033): error opening trace file: No such file or directory (2)

I tried moving the ViewPager to the top level content (i.e. setContent( pager )) but that didn't seem to work. Any input is welcomed. The code is below:

EDIT* I Simplified the example, and I still get the same problem.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AskQuestionActivity extends FragmentActivity
{
    private static LayoutParams MATCH_PARENT = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT );

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        ViewPager pager = new ViewPager( this );
        pager.setAdapter( new FragmentPagerAdapter( getSupportFragmentManager() )
        {
            @Override
            public int getCount()
            {
                return 1;
            }

            @Override
            public Fragment getItem( int arg0 )
            {
                return new BoardFragment();
            }
        } );

        setContentView( pager );
    }

    public static class BoardFragment extends Fragment
    {
        @Override
        public View onCreateView( LayoutInflater inflater,
                                  ViewGroup container,
                                  Bundle savedInstanceState )
        {
            TextView textView = new TextView( getActivity() );
            textView.setText( "Board" );
            textView.setGravity( Gravity.CENTER );

            LinearLayout layout = new LinearLayout( getActivity() );
            layout.setLayoutParams( MATCH_PARENT );
            layout.addView( textView );

            return layout;
        }
    }
}

Any help is welcomed.

Bitterweed answered 7/3, 2013 at 2:59 Comment(12)
I don't think the problem is in the code you've shown. Do you have any XML layout files you're using?Westmoreland
No, not in this case. Unless my manifest would come in handy (i know, i should probably put my layouts in XML, but im just playing around with some stuff). For a second, i thought it might be because I haven't given my viewpager a size by calling pager.setLayoutParam( params ); but that didn't do the trick either.Bitterweed
Something is being treated as a resource when it should be treated as a color. The thing I see, though, is that you don't have any resources referenced in this code. (I think, somewhere, 0xFFFFFFFF is used instead of android.R.color.white, or similar.) The reason I suggested XML is because the stack trace doesn't link back to your code; but it may be from a Fragment auto-inflation or something.Westmoreland
I actually have a different theory, I think instantiating a ViewPager like I have results in the problem (PS, i updated the example to be a bit more simple). I have a gut feeling that the FragmentManager makes some assumptions that a view pager will always be in a layout xml and not programmatically passed (like I've done). I'll try creating a layout file and seeing if that works.Bitterweed
That would be very odd, as I have an app which instantiates the ViewPager programmatically as well. I've never had an issue like this before.Westmoreland
Ok, well these are my final observations. I wish I knew more about the insides of android to explain what im seeing, but here it is. By putting the ViewPager in an XML alone, i get errors (unbound prefix..??), wrapping the ViewPager in a LinearLayout and then getting the ViewPager using findViewById seems to solve my problem. So Maybe the above has a couple a problems, the first of which is that the ViewPager cannot be added as a top level view (it must be a child view of a view group), secondly, maybe it needs to have an ID? Maybe thats what the FragmentManager expects?Bitterweed
Unbound prefix means you didn't include the xmlns:android="..." line in your ViewPager (it will be with your LinearLayout, though).Westmoreland
You're right. Ignore my comment about requiring a parent. However, I still can't seem to make it work by instantiating the view pager programmatically, however, doing it through XML seems to work.Bitterweed
That's odd. I really don't know why.Westmoreland
Ok, Not that I want to perpetuate a bad practice, but i just added this line to the code pager.setId( 453543 ); and it seemed to work.. so maybe my theory of needing an ID is the right one? I don't know.Bitterweed
This is likely related to the type of adapter you are binding to your ViewPager. I have exactly the same problem since I started to use a FragmentStatePagerAdapter instead of a simple PagerAdapter-subclass. I will now try to include the ViewPager per layout xml. Thank you!Voccola
Setting ID worked for me.Aquifer
B
86

So I'll post what I think is the solution to my problem. After adding a single line to my above code giving the ViewPager a "random" ID; it managed to fix my problem.

At first I was convinced that setting your own ID is bad practice, but apparently there are legitimate ways of doing this. Namely by creating an ids.xml file and letting the Android ecosystem generate an ID for you. I recommend checking out these SO posts for more information - here and here.

Otherwise, I would recommend just creating an XML file for your layout and getting the ViewPager in your Activity (instead of programmatically instantiating it), but ultimately it all depends on what you're trying to accomplish.

Bitterweed answered 7/3, 2013 at 15:9 Comment(4)
why does the viewPager need an ID? why does it cause a crash in case you don't provide it with one?Hirai
@androiddeveloper my guess is, in the support library, either by design, or as a bug, the fragment manager requires the top level view group to have IDs. If you figure out why, feel free to post an update.Bitterweed
but it's a viewPager. it already has all that it needs...it's already inside the views-"tree", and it already can create fragments via an adapter... weird.Hirai
If you're using API 17 or later, you can call View.generateViewId() to generate a unique ID that won't conflict with any generated by your layout files.Casework
A
22
ViewPager pager = new ViewPager(this);

need set resource id

pager.setId(0x1000);
Aiguille answered 17/7, 2013 at 7:58 Comment(0)
N
2

Now in 2021 this problem hit me in my Xamarin Forms with a AppShell that was working perfectly, but after upgraded to the last version of many Google AndroidX Nuget Packages started to crash with the message error of this post when opened the menu to change to another page. The solution in my case: I downgraded to the previous version all the libraries one by one and the culprit seems to be the Xamarin.AndroidX.Legacy.Support.V4. The v1.0.0.6 was the last one that worked without problems for me. I don't know the reason.

Newlywed answered 4/6, 2021 at 4:32 Comment(0)
R
1

I was having a similar problem with XML layout files that were under the layout folder. The code used to work in eclipse but when the project was moved to Android Studio I kept getting the android.content.res.Resources$NotFoundException.

To fix the problem I ended up renaming the XML files that were causing the problem and that solved the problem. Before that I tried rebuilding the project, making modifications to the XML file but none of that worked.

Reverie answered 10/9, 2013 at 11:29 Comment(0)
W
1

Exactly same problem. I programmatically generated a viewpager in a fragment. when the fragment back to foreground it crashed.

Even though, I already generated a random id for the view pager it still crash. The only way is assign a fixed Id to it. Weird!

Wiersma answered 25/11, 2015 at 1:1 Comment(2)
That is because Fragments needs a tag or id to be. If they don't have it they use the container id. If the container has no id, the fragment crashesArtemisia
Yes, thank you. In order to avoid those kinds of problem, I use a id generater to assgin ID to each programmatically created item in runtime.Wiersma
M
0

I had the same error. Mine was caused by my Adapter class having one extra textView that did not have a matching imageView. I had 7 TextViews and only 6 ImageViews. It took me a couple hours to figure out a simple mistake.

Michelson answered 6/2, 2014 at 18:9 Comment(0)
H
0

I had the same issue. The problem was the missing layout file. I had put the layout in layout-xlarge folder but not the layout folder. So small screen tablet had that issue. This might give some people a clue.

Hornback answered 21/7, 2015 at 6:14 Comment(0)
Q
0

I get the same exception when I set adapter viewPager.setAdapter(adapter);, only in Android KitKat 4.4. I try almost all solutions but not working.

In my case, soluation was when I move .xml file drawable-nodpi to drawable folder (which was some SVG images), and Build -> Clean Project then error gone.

Quisling answered 20/7, 2017 at 6:28 Comment(0)
S
0

I was trying to set an Int value to the TextView directly.

Resolved by converting the value to String before setting it to the TextView.

Staggers answered 4/5, 2020 at 7:29 Comment(0)
I
0

I know this is an old question, but maybe the solution I found can help anyone with a similar problem...

The error message I got was the cryptic 'Android.Content.Res.Resources+NotFoundException: 'Unable to find resource ID #0x0' - only 'resource ID' is different from the origional question.

After a lot of research, I finally figured out that the crash had to do with a call to PushModalAsync(), although I had no idea why...

The solution that finally solved this issue for me can be found in the styles.xml file. I had assigned a value of #00ffffff to the android:windowBackground. It turns out that any hex color in <item name="android:windowBackground">#000000</item> will throw this exception.

The solution is to assign the hex color in the colors.xml file, and then reference this color in styles.xml like <item name="android:windowBackground">@color/window_background</item>

This is the 'bug' report that got me on the right track... Exception thrown on PushModalAsync() when android:windowBackground set as HEX

Interlocutrix answered 30/12, 2021 at 5:47 Comment(0)
D
0

I ran into this error recently when I was trying to use the Navigation Component in Android like so:

<fragment
    android:id="@+id/my_fragment"
    android:label="My Fragment"
    android:name="com.example.www"

    <action
        android:id="@+id/action_switch_app"
        app:destination="@id/switch_app"
        app:enterAnim="@animator/fragment_open_enter" 
        app:popEnterAnim="@animator/fragment_open_enter" 
        app:popExitAnim="@animator/fragment_fade_exit"/>

</fragment>

<activity 
    android:id="@+id/switch_app"
    android:action="android.intent.action.VIEW"
    android:data="https://another_app.com"
/>

The fragment animations do not belong in the action tag where the destination is an activity in another app.

Dorcy answered 14/7, 2022 at 9:1 Comment(0)
A
0

REMOVE app/build package.

When you change id view delete app/build package.


OR: Rebuild Project

enter image description here


Arjan answered 1/9, 2022 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.