SmallRecordDetailsWidget

SmallRecordDetailsWidget

我在尝试创建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的构造函数中的一行。充气机即使在简单的简单布局下也无法运行,就像这样

    <?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的构造函数膨胀的布局文件中定义它们(作为根元素的子代),那么它们也应该膨胀,以便以后可以通过。



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

堆栈跟踪为:

        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


如我发布的代码所述,在findViewById(...)的构造函数中,我为布局SmallRecordDetailsWidget充气,其中R.layout.small_record_details是定义小部件的xml(而small_record_details.xml是其根标记的名称)。

只是为了阐明我的目标-我想创建一个包含其他一些小部件。我希望在xml中为“复合窗口小部件”定义所有它们,然后在构造函数中获取对子窗口小部件的引用,以便客户端可以执行com.lexues.android.hari_prototype.views.SmallRecordDetailsWidget等操作。

最佳答案

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

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

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


当您从片段中放大/实例化自定义布局时,可以使用该标签,而在放大小视图层次结构并将其附加到自定义布局时,则不能使用该标签。视图层次结构不应包含该标签,请尝试在layout / small_record_details.xml中放置类似TextView的内容

07-26 09:31