对LinearLayout进行子类化

时间:2014-08-29 08:37:36

标签: android android-linearlayout

我在尝试创建LinearLayout的子类时遇到问题。我有一个带有这种布局的简单片段

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map_container"
            >

    <!-- Map view -->
    <org.osmdroid.views.MapView
        android:id="@+id/fragment_map_map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"/>


    <!-- Record description -->
    <com.example.SmallRecordDetailsWidget
        android:id="@+id/fragment_map_record_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</FrameLayout>

SmallRecordDetailsWidget是LinearLayout的子类。 我的问题是我无法找到实例化/充气的正确方法。

这是我的SmallRecordDetailsWidget现在的样子

public class SmallRecordDetailsWidget extends LinearLayout {


    private final int layoutID = R.layout.small_record_details;

    public SmallRecordDetailsWidget(Context context, AttributeSet attrs) {

        super(context, attrs);
        LayoutInflater.from(context).inflate(this.layoutID, this, true) ;

    }


}

然后在片段'onCreateView中我做了:

@Override
    public View onCreateView(
        LayoutInflater inflater, ViewGroup containter, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_map, containter, false) ;

        this.mapView = (MapView)( view.findViewById(R.id.fragment_map_map_view) );
        this.recordDetailsWidget = (SmallRecordDetailsWidget)( view.findViewById(R.id.fragment_map_record_detail) ) ;

        return view;

    }

但是这个代码在View view = inflater.inflate(...)上用

制动
android.view.InflateException: Binary XML file line #18: Error inflating class com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget

我可以跟踪被抛出

LayoutInflater.from(context).inflate(this.layoutID, this, true) ;

来自SmallRecordDetailsWidget的构造函数的行。 Inflater甚至在死的简单布局上失败,就像这个

    <?xml version="1.0" encoding="utf-8"?>

    <!-- A widget with details of selected point of interest. This will be placed at the bottom of the map widget -->
    <com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/small_record_details_layout"
            android:layout_width="match_parent"
            android:layout_height="82dp">

    </com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget>

现在,我已经将我的问题剥离到最小的情况下再现了这个问题,所以看起来我没有在这里做任何有用的事情,但目标是SmallRecordDetailsWidget有几个简单的成员小部件,然后与他们做一些工作。如果我理解正确,只要它们在布局文件中被定义SmallRecordDetailsWidget的构造函数膨胀(作为根元素的子元素)那么它们也应该被夸大,以便我以后可以得到保持其中findViewById(...)


编辑 - 添加了堆栈跟踪和一些额外的细节

堆栈跟踪是:

        09-01 09:24:18.661    6330-6330/com.lexues.android.hari_prototype E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.lexues.android.hari_prototype, PID: 6330
        android.view.InflateException: Binary XML file line #18: Error inflating class com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget
        at android.view.LayoutInflater.createView(LayoutInflater.java:626)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.lexues.android.hari_prototype.controllers.fragments.MapFragment.onCreateView(MapFragment.java:87)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
        at android.view.View.measure(View.java:17396)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17396)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
        at android.view.View.measure(View.java:17396)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
        at android.view.View.measure(View.java:17396)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
        at android.view.Choreographer.doCallbacks(Choreographer.java:603)
        at android.view.Choreographer.doFrame(Choreographer.java:573)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5335)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at android.view.LayoutInflater.createView(LayoutInflater.java:600)
                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                    at com.lexues.android.hari_prototype.controllers.fragments.MapFragment.onCreateView(MapFragment.java:87)
                    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
                    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
                    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
                    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
                    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
                    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
                    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
                    at android.view.View.measure(View.java:17396)
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                    at android.view.View.measure(View.java:17396)
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
                    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
                    at android.view.View.measure(View.java:17396)
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
                    at android.view.View.measure(View.java:17396)
                    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175)
                    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
                    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
                    at android.view.Choreographer.doFrame(Choreographer.java:573)
                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
                    at android.os.Handler.handleCallback(Handler.java:733)
                    at android.os.Handler.dispatchMessage(Handler.java:95)
                    at android.os.Looper.loop(Looper.java:157)
                    at android.app.ActivityThread.main(ActivityThread.java:5335)
                    at java.lang.reflect.Method.invokeNative(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:515)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                    at dalvik.system.NativeStart.main(Native Method)

        Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:626)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.lexues.android.hari_prototype.views.SmallRecor
        09-01 09:24:19.381     882-6350/? E/android.os.Debug﹕ !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
        09-01 09:24:20.361      267-267/? E/SMD﹕ DCD ON

正如我发布的代码中所述,在SmallRecordDetailsWidget的构造函数中,我膨胀了布局R.layout.small_record_details,其中small_record_details.xml是定义窗口小部件的xml(而com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget是它的根标签名称。)

只是为了明确我的目标 - 我想创建一个包含其他一些内容的小部件。我希望在xml中定义所有这些&#39;复合小部件&#39;然后在costructor中获取对子窗口小部件的引用,以便客户端可以执行SmallRecordDetailsWidget.titleTextWidget.setText(...)等操作。

2 个答案:

答案 0 :(得分:3)

阅读你的问题,似乎你在SmallRecordDetailsWidget的构造函数中夸大错误的布局

在那里,你不应该使用标签

<com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget>

当您从片段中膨胀/设置自定义布局时,可以使用该标记,但是当您为小视图层次结构充气并将其附加到自定义布局时,则不会。视图层次结构不应包含该标记,尝试在layout / small_record_details.xml中放置类似TextView的内容

答案 1 :(得分:0)

因此,在使用更好的关键字(使用custom View而非extending ViewGroup进行Google搜索)之后,本文帮助了我: http://www.tiemenschut.com/how-to-create-compound-controls-for-android/

我的问题是我使用com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget作为定义我的自定义窗口小部件的布局的根元素的名称。我应该根据需要简单地使用LinearLayout或任何其他布局。有意义的是考虑它,否则我在完成定义之前引用该元素。傻我。