XML Inflater没有看到任何视图?

时间:2011-02-07 23:28:29

标签: android xml views inflate

首先,我应该多问一下我是否确实可以做我想做的事。

我有一个自定义RelativeView,它包含4个TextViews和一个按xml布局定义的按钮。

<?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>

我将这些视图扩展到了RelativeLayout类。

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);
    }

到目前为止,一切看起来都不错,所以我运行它并得到一个巨大的xml崩溃说它无法找到我的资源。是因为我将它嵌入到自定义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

感谢您的帮助 〜Aedon

EDIT @ DAN BRESLAU堆栈跟踪:

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

这是班上的第一部分。它必须处理初始化布局。

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);
    }

3 个答案:

答案 0 :(得分:4)

正如mreichelt在评论中指出的那样,您对Layout.inflate的使用似乎很可疑。你可能想做更像这样的事情:

    // 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);

等等。请特别注意,传递到inflater.inflate的ID前缀为R.layout.,而传入findViewById的ID前缀为R.id.(不幸的是,传递错误类型的资源标识符将会永远不会生成编译时错误。)

答案 1 :(得分:1)

我认为您需要将第一个标记定义为<merge> - 标记,而不是再次定义自己的类。至少我几天前这样做了。以下是更多信息: http://developer.android.com/resources/articles/layout-tricks-merge.html

答案 2 :(得分:0)

你做错了,你只为根视图调用inflate。

com.android.appionresourcemanager.Widgets.Digital mDigital = (Digital)View.inflate(this, R.layout.mylayout, null);
 mReadDisplay = (TextView)mDigital.findViewById(R.id.digi_reading);
 //....

希望这有帮助!