XML Inflater not seeing any of the views?

…衆ロ難τιáo~ 提交于 2019-12-19 11:36:27

问题


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.

<?xml version="1.0" encoding="utf-8"?>
<view class="com.android.appionresourcemanager.Widgets.Digital"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/digitalface">
      <TextView android:id="@+id/digi_reading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"/>
      <TextView android:id="@+id/digi_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"/> 
      <Button android:id="@+id/digi_grouping"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"/>
      <TextView android:id="@+id/digi_measurement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
      <!-- If a gauge has more than one input types, this is how they would 
      toggle them. -->
      <TextView android:id="@+id/digi_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/digi_reading"
        android:layout_above="@+id/digi_measurement"/>
</view>

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.<init>(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.<init>(AppionWidget.java:30)
02-07 16:23:57.332: ERROR/AndroidRuntime(28002):     at com.android.appionresourcemanager.Widgets.Gauge.<init>(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.<init>(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 <unknown>
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.<init>(AppionWidget.java:30)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.Gauge.<init>(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.<init>(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 <unknown>
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.<init>(AppionWidget.java:30)
02-08 08:52:21.904: ERROR/AndroidRuntime(1393):     at com.android.appionresourcemanager.Widgets.Gauge.<init>(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.<init>(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);
    }

回答1:


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 with R.layout., whereas the ID passed into findViewById is prefixed with R.id. (Unfortunately, passing the wrong kind of resource identifier will never generate a compile-time error.)




回答2:


I think you need to define the first tag as <merge>-tag instead of defining your own class again. At least I did it this way a few days ago. Here is more information: http://developer.android.com/resources/articles/layout-tricks-merge.html




回答3:


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!



来源:https://stackoverflow.com/questions/4927919/xml-inflater-not-seeing-any-of-the-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!