title: Android: Theme.resolveAttribute returns 0 cause Resources$NotFoundException: Resource ID #0x0
body: <p><strong>Short Question:</strong> <code>Resources.Theme.resolveAttibute(attrId, typVal, true)</code> returns <code>typVal.resourceId</code> is <strong>0</strong>, cause that exception. This <code>ResourceReader.getResourceId()</code> works well in all places of whole app. But randomly, app update or after several restarts this exception occured because TypedValue.resourceId is 0.</p>
  
  <p><strong>Long Question:</strong>
  I'm getting <em>ResourcesNotFoundException</em> on app <strong>restart</strong> unfortunately I can't determine when this error occurs. When I restart (close, kill, restart, update, setup) my app several times in a minute at last my app gives this RNFE on first resource read line.</p>
  
  <p>Stack trace:</p>
  
  <pre><code> Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
          at android.content.res.Resources.getValue(Resources.java:2331)
          at android.content.res.Resources.getColor(Resources.java:2013)
          at com.my.app.navi.NavigationFragment.setUp(NavigationDrawerFragment.java:570)
          at com.my.app.main.MainActivity.onCreate(MainActivity.java:1071)
  </code></pre>
  
  <p>It's hard to generate that exception but in Google Play Developer Console I can see a lot of exception generated by other devices.</p>
  
  <p>I looked up a lot answers for that exception but nor rebuild, <code>String.ValueOf(int)</code>, resource add solved my problem.</p>
  
  <p>I have several build flavors for different styled and featured app. Because of that I use attr style reference for resources. All flavors has different res folders. main res folders has attrs.xml for reference colors, drawables and images. All the other code and xml layout files has R.attrs references</p>
  
  <p>For explain my situation clearly this is main res attrs.xml</p>
  
  <pre><code>&lt;declare-styleable name="Colors"&gt;
      &lt;!--&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;  COLORS  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;--&gt;
      &lt;attr name="c_pressed_button_back" format="color"/&gt;
      &lt;attr name="c_window_back" format="color"/&gt;
      &lt;attr name="c_navigation_drawer_back" format="color"/&gt;
  </code></pre>
  
  <p>flavor_one style.xml</p>
  
  <pre><code>&lt;!--&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;  COLORS  &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;--&gt;
      &lt;item name="c_pressed_button_back"&gt;@color/c_theme_pressed_button_back&lt;/item&gt;
      &lt;item name="c_window_back"&gt;@color/c_theme_window_back&lt;/item&gt;
      &lt;item name="c_navigation_drawer_back"&gt;@color/c_theme_navigation_drawer_back&lt;/item&gt;
  </code></pre>
  
  <p>flavor_one colors.xml</p>
  
  <pre><code>&lt;color name="c_theme_pressed_button_back"&gt;@color/c_theme_red&lt;/color&gt;
  &lt;color name="c_theme_window_back"&gt;@color/c_theme_green&lt;/color&gt;
  &lt;color name="c_theme_navigation_drawer_back"&gt;@color/c_theme_blue&lt;/color&gt;
  </code></pre>
  
  <p>With these settings I wrote this class for resource reading.</p>
  
  <pre><code>public class ResourceReader
  {
      public static int getResourceId(Resources.Theme thm, int attrId)
      {
          int returnValue = 0;
          TypedValue typedValue = new TypedValue();
          try {
              thm.resolveAttribute(attrId, typedValue, true);
              returnValue = typedValue.resourceId;
          }
          catch (Exception ex) // Resource cannot resolved
          {
              ex.printStackTrace();
          }
          if(returnValue == 0)
          {
              Log.e("yucel", "Resource resolved result is 0  @thm:" + thm + "  attrid:" + attrId + "  typdata:" + typedValue.data + " typtyp:" + typedValue.type + " ");
          }
      return returnValue;
      }
  }
  </code></pre>
  
  <p>And finally I use resources like</p>
  
  <pre><code>    int back_color_id = ResourceReader.getResourceId(appData.appContext.getTheme(), R.attr.c_navigation_drawer_back);
      int back_color=  appData.appRes.getColor(back_color_id);
      mDrawerLayout.setColor(back_color);
  </code></pre>
  
  <p>At the line of <code>getColor(back_color_id)</code> exception will be thrown. Because of back_color_id is equal to 0. In resource reader <code>thm.resolveAttribute(attrId, typedValue, true);</code></p>
  
  <p>I think whole code statements looks ok. Because whole app and 99 percent of use this structure works well. Problem occurs after several restarts and randomly at resource reading. Something must be changed in that situtation. Is there any method to check is resources in a proper state like <code>Context.isResourceLoading()</code> or <code>Resoureces.isLoaded()</code>?</p>
  
  <p>Is there is any one faced resource reading problem at randomly, after several restart or after updates?</p>