XML Inflater not seeing any of the views?-Collection of common programming errors
First I should prolly ask if it is indeed possible to do what I’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, this, true);
mReadDisplay.setTypeface(Typeface.createFromAsset(context.getAssets(),
"font/DIGITALDREAM.ttf"));
mReadDisplay.setTextSize(18f);
mHeadDisplay = (TextView)inflater.inflate(R.id.digi_header, this, true);
mHeadDisplay.setTextSize(14f);
mMeasureDisplay = (TextView)inflater.inflate(R.id.digi_measurement, this, true);
mMeasureDisplay.setTextSize(10f);
mTypeDisplay = (TextView)inflater.inflate(R.id.digi_type, this, true);
mTypeDisplay.setTextSize(10f);
mGrouping = (Button)inflater.inflate(R.id.digi_grouping, this, true);
mGrouping.setBackgroundColor(0x00000000);
}
Everything looks good so far, so I run it and get a massive xml crash saying that it can’t find my resource. Is it because I embedded it in the custom RelativeLayout?
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
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.View.inflate(View.java:8666)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.appionresourcemanager.Widgets.WorkBench$2$1.onClick(WorkBench.java:64)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:876)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.widget.ListView.performItemClick(ListView.java:3382)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.os.Handler.handleCallback(Handler.java:587)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.os.Looper.loop(Looper.java:144)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at java.lang.reflect.Method.invoke(Method.java:521)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at dalvik.system.NativeStart.main(Native Method)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): Caused by: java.lang.reflect.InvocationTargetException
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at java.lang.reflect.Constructor.constructNative(Native Method)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): ... 19 more
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050000 type #0x12 is not valid
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.content.res.Resources.getLayout(Resources.java:731)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.appionresourcemanager.Widgets.DigitalGauge.init(DigitalGauge.java:47)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.appionresourcemanager.Widgets.AppionWidget.(AppionWidget.java:30)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): at com.android.appionresourcemanager.Widgets.Gauge.(Gauge.java:13)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002): ... 23 more
Thanks for any help ~Aedon
EDIT @ DAN BRESLAU The stack trace:
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
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.View.inflate(View.java:8666)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.WorkBench$3$1.onClick(WorkBench.java:106)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:876)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.widget.ListView.performItemClick(ListView.java:3382)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.os.Handler.handleCallback(Handler.java:587)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.os.Looper.loop(Looper.java:144)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Method.invoke(Method.java:521)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at dalvik.system.NativeStart.main(Native Method)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): Caused by: java.lang.reflect.InvocationTargetException
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Constructor.constructNative(Native Method)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): ... 19 more
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.View.inflate(View.java:8666)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.DigitalGauge.init(DigitalGauge.java:45)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.AppionWidget.(AppionWidget.java:30)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.Gauge.(Gauge.java:13)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): ... 23 more
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): Caused by: java.lang.reflect.InvocationTargetException
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Constructor.constructNative(Native Method)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): ... 31 more
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.View.inflate(View.java:8666)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.DigitalGauge.init(DigitalGauge.java:45)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.AppionWidget.(AppionWidget.java:30)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.Gauge.(Gauge.java:13)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): ... 35 more
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): Caused by: java.lang.reflect.InvocationTargetException
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at com.android.appionresourcemanager.Widgets.DigitalGauge.(DigitalGauge.java:41)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Constructor.constructNative(Native Method)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): ... 43 more
02-08 08:52:21.904: ERROR/AndroidRuntime(1393): Caused by: android.view.InflateException: B
And this is the first part of the class. It has to deal with initializing the layout.
public class Digital extends RelativeLayout{
/***** ***** ***** ***** ***** ***** ***** *****\
* BEGIN ALL CONSTANTS AND VARIALBES
\***** ***** ***** ***** ***** ***** ***** *****/
// Class name for logging purposes
private final String TAG = "Digital";
/**
* All of the display types used for the Digital Gauge display
*/
private TextView mReadDisplay, mHeadDisplay, mMeasureDisplay, mTypeDisplay;
/**
* The grouping that the gauge is in. Clear if none. Click-able to reassign
*/
private Button mGrouping;
/** The current type of the gauge the we will display. */
private int mType = 0;
public Digital(Context context, AttributeSet attrs){
super(context, attrs);
}
public void init() {
Digital mDigital = (Digital)View.inflate(context, R.layout.digital, null);
mReadDisplay = (TextView)mDigital.findViewById(R.id.digi_reading);
mReadDisplay.setTypeface(Typeface.createFromAsset(context.getAssets(),
"font/DIGITALDREAM.ttf"));
mReadDisplay.setTextSize(18f);
mHeadDisplay = (TextView)mDigital.findViewById(R.id.digi_header);
mHeadDisplay.setTextSize(14f);
mMeasureDisplay = (TextView)mDigital.findViewById(R.id.digi_measurement);
mMeasureDisplay.setTextSize(10f);
mTypeDisplay = (TextView)mDigital.findViewById(R.id.digi_type);
mTypeDisplay.setTextSize(10f);
mGrouping = (Button)mDigital.findViewById(R.id.digi_grouping);
mGrouping.setBackgroundColor(0x00000000);
}
-
As mreichelt pointed out in a comment, your use of Layout.inflate seems suspicious. You probably want to do something more like this:
// Assume that the XML file from your question (above) is named MyLayoutFile.xml // Digital is your derived layout class Digital layout = (Digital) inflater.inflate(R.layout.MyLayoutFile, this, true); mReadDisplay = (TextView) layout.findViewById(R.id.digi_reading); //... mHeadDisplay = (TextView) layout.findViewById(R.id.digi_header); //... mMeasureDisplay = (TextView) layout.findViewById(R.id.digi_measurement);
and so on. Notice especially that the ID passed into
inflater.inflate
is prefixed withR.layout.
, whereas the ID passed intofindViewById
is prefixed withR.id.
(Unfortunately, passing the wrong kind of resource identifier will never generate a compile-time error.) -
You are doing it wrong, you only call inflate for the root view.
com.android.appionresourcemanager.Widgets.Digital mDigital = (Digital)View.inflate(this, R.layout.mylayout, null); mReadDisplay = (TextView)mDigital.findViewById(R.id.digi_reading); //....
hope this helps!
Originally posted 2013-11-09 23:19:34.