{"id":1051,"date":"2022-08-30T15:11:34","date_gmt":"2022-08-30T15:11:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/xml-inflater-not-seeing-any-of-the-views-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:34","modified_gmt":"2022-08-30T15:11:34","slug":"xml-inflater-not-seeing-any-of-the-views-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/xml-inflater-not-seeing-any-of-the-views-collection-of-common-programming-errors\/","title":{"rendered":"XML Inflater not seeing any of the views?-Collection of common programming errors"},"content":{"rendered":"<p>First I should prolly ask if it is indeed possible to do what I&#8217;m trying to do.<\/p>\n<p>I have a custom RelativeView and it contains 4 TextViews and a button defines in an xml layout.<\/p>\n<pre><code>\n\n      \n       \n      \n      \n      \n      \n\n<\/code><\/pre>\n<p>This I inflated the views into the RelativeLayout class.<\/p>\n<pre><code>public void init() {\n        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);\n\n        mReadDisplay = (TextView)inflater.inflate(R.id.digi_reading, this, true);\n            mReadDisplay.setTypeface(Typeface.createFromAsset(context.getAssets(),\n                    \"font\/DIGITALDREAM.ttf\"));\n            mReadDisplay.setTextSize(18f);\n        mHeadDisplay = (TextView)inflater.inflate(R.id.digi_header, this, true);\n            mHeadDisplay.setTextSize(14f);\n        mMeasureDisplay = (TextView)inflater.inflate(R.id.digi_measurement, this, true);\n            mMeasureDisplay.setTextSize(10f);\n        mTypeDisplay = (TextView)inflater.inflate(R.id.digi_type, this, true);\n            mTypeDisplay.setTextSize(10f);\n        mGrouping = (Button)inflater.inflate(R.id.digi_grouping, this, true);\n            mGrouping.setBackgroundColor(0x00000000);\n    }\n<\/code><\/pre>\n<p>Everything looks good so far, so I run it and get a massive xml crash saying that it can&#8217;t find my resource. Is it because I embedded it in the custom RelativeLayout?<\/p>\n<pre><code>02-07 16:23:57.332: ERROR\/AndroidRuntime(28002): android.view.InflateException: Binary XML file line #2: Error inflating class com.android.appionresourcemanager.Widgets.DigitalGauge\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.View.inflate(View.java:8666)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.appionresourcemanager.Widgets.WorkBench$2$1.onClick(WorkBench.java:64)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:876)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.widget.ListView.performItemClick(ListView.java:3382)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.os.Handler.handleCallback(Handler.java:587)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.os.Handler.dispatchMessage(Handler.java:92)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.os.Looper.loop(Looper.java:144)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.app.ActivityThread.main(ActivityThread.java:4937)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at java.lang.reflect.Method.invokeNative(Native Method)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at java.lang.reflect.Method.invoke(Method.java:521)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at dalvik.system.NativeStart.main(Native Method)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002): Caused by: java.lang.reflect.InvocationTargetException\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at java.lang.reflect.Constructor.constructNative(Native Method)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     ... 19 more\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050000 type #0x12 is not valid\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.content.res.Resources.getLayout(Resources.java:731)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.appionresourcemanager.Widgets.DigitalGauge.init(DigitalGauge.java:47)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.appionresourcemanager.Widgets.AppionWidget.(AppionWidget.java:30)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     at com.android.appionresourcemanager.Widgets.Gauge.(Gauge.java:13)\n02-07 16:23:57.332: ERROR\/AndroidRuntime(28002):     ... 23 more\n<\/code><\/pre>\n<p>Thanks for any help ~Aedon<\/p>\n<p>EDIT @ DAN BRESLAU The stack trace:<\/p>\n<pre><code>02-08 08:52:21.904: ERROR\/AndroidRuntime(1393): android.view.InflateException: Binary XML file line #2: Error inflating class com.android.appionresourcemanager.Widgets.DigitalGauge\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.View.inflate(View.java:8666)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.WorkBench$3$1.onClick(WorkBench.java:106)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:876)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.widget.ListView.performItemClick(ListView.java:3382)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.os.Handler.handleCallback(Handler.java:587)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.os.Handler.dispatchMessage(Handler.java:92)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.os.Looper.loop(Looper.java:144)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.app.ActivityThread.main(ActivityThread.java:4937)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Method.invokeNative(Native Method)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Method.invoke(Method.java:521)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at dalvik.system.NativeStart.main(Native Method)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393): Caused by: java.lang.reflect.InvocationTargetException\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Constructor.constructNative(Native Method)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     ... 19 more\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class \n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.View.inflate(View.java:8666)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.DigitalGauge.init(DigitalGauge.java:45)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.AppionWidget.(AppionWidget.java:30)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.Gauge.(Gauge.java:13)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     ... 23 more\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393): Caused by: java.lang.reflect.InvocationTargetException\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Constructor.constructNative(Native Method)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     ... 31 more\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class \n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.View.inflate(View.java:8666)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.DigitalGauge.init(DigitalGauge.java:45)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.AppionWidget.(AppionWidget.java:30)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.Gauge.(Gauge.java:13)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     ... 35 more\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393): Caused by: java.lang.reflect.InvocationTargetException\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Constructor.constructNative(Native Method)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393):     ... 43 more\n02-08 08:52:21.904: ERROR\/AndroidRuntime(1393): Caused by: android.view.InflateException: B\n<\/code><\/pre>\n<p>And this is the first part of the class. It has to deal with initializing the layout.<\/p>\n<pre><code>public class Digital extends RelativeLayout{\n    \/*****  *****   *****   *****   *****   *****   *****   *****\\\n     * BEGIN ALL CONSTANTS AND VARIALBES \n    \\*****  *****   *****   *****   *****   *****   *****   *****\/\n    \/\/ Class name for logging purposes\n    private final String TAG    =   \"Digital\";\n    \/**\n     * All of the display types used for the Digital Gauge display\n     *\/\n    private TextView mReadDisplay, mHeadDisplay, mMeasureDisplay, mTypeDisplay;\n    \/**\n     * The grouping that the gauge is in. Clear if none. Click-able to reassign\n     *\/\n    private Button mGrouping;   \n    \/** The current type of the gauge the we will display. *\/\n    private int mType = 0;\n\n    public Digital(Context context, AttributeSet attrs){\n        super(context, attrs);\n    }\n\n    public void init() {\n        Digital mDigital = (Digital)View.inflate(context, R.layout.digital, null);\n        mReadDisplay = (TextView)mDigital.findViewById(R.id.digi_reading);\n            mReadDisplay.setTypeface(Typeface.createFromAsset(context.getAssets(),\n                    \"font\/DIGITALDREAM.ttf\"));\n            mReadDisplay.setTextSize(18f);\n        mHeadDisplay = (TextView)mDigital.findViewById(R.id.digi_header);\n            mHeadDisplay.setTextSize(14f);\n        mMeasureDisplay = (TextView)mDigital.findViewById(R.id.digi_measurement);\n            mMeasureDisplay.setTextSize(10f);\n        mTypeDisplay = (TextView)mDigital.findViewById(R.id.digi_type);\n            mTypeDisplay.setTextSize(10f);\n        mGrouping = (Button)mDigital.findViewById(R.id.digi_grouping);\n            mGrouping.setBackgroundColor(0x00000000);\n    }\n<\/code><\/pre>\n<ol>\n<li>\n<p>As mreichelt pointed out in a comment, your use of Layout.inflate seems suspicious. You probably want to do something more like this:<\/p>\n<pre><code>    \/\/ Assume that the XML file from your question (above) is named MyLayoutFile.xml \n    \/\/ Digital is your derived layout class\n    Digital layout = (Digital) inflater.inflate(R.layout.MyLayoutFile, this, true);\n\n    mReadDisplay = (TextView) layout.findViewById(R.id.digi_reading);\n    \/\/...\n    mHeadDisplay = (TextView) layout.findViewById(R.id.digi_header);\n    \/\/...\n    mMeasureDisplay = (TextView) layout.findViewById(R.id.digi_measurement);\n<\/code><\/pre>\n<p>and so on. Notice especially that the ID passed into <code>inflater.inflate<\/code> is prefixed with <code>R.layout.<\/code>, whereas the ID passed into <code>findViewById<\/code> is prefixed with <code>R.id.<\/code> (Unfortunately, passing the wrong kind of resource identifier will never generate a compile-time error.)<\/p>\n<\/li>\n<li>\n<p>You are doing it wrong, you only call inflate for the root view.<\/p>\n<pre><code>com.android.appionresourcemanager.Widgets.Digital mDigital = (Digital)View.inflate(this, R.layout.mylayout, null);\n mReadDisplay = (TextView)mDigital.findViewById(R.id.digi_reading);\n \/\/....\n<\/code><\/pre>\n<p>hope this helps!<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:19:34. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>First I should prolly ask if it is indeed possible to do what I&#8217;m trying to do. I have a custom RelativeView and it contains 4 TextViews and a button defines in an xml layout. This I inflated the views into the RelativeLayout class. public void init() { LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mReadDisplay = (TextView)inflater.inflate(R.id.digi_reading, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1051","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1051"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1051\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}