Android : Subclassing LinearLayout

on Friday, August 29, 2014


I have a problem trying to create a subclass of LinearLayout. I have a simple fragment with this layout



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


The SmallRecordDetailsWidget is a subclass of LinearLayout. My problem is that I can't find the right way to instantiate/inflate it.


Here's what my SmallRecordDetailsWidget looks like now



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

}


}


Then in fragment's onCreateView I'm doing:



@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;

}


But this codes brakes on View view = inflater.inflate(...) with



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


which I can track to being thrown on


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


line from SmallRecordDetailsWidget's constructor. Inflater fails even on dead simple layout, like this one



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


Now, I've stripped my problem to smallest case that reproduces the issue, so it looks like I'm not doing anything useful here, but the goal is for SmallRecordDetailsWidget to have a few simple member widgets and then do a bit of work with them. If I understand correctly as long as they would be defined within layout file that SmallRecordDetailsWidget's constructor inflates (as children of the root element) then they should also get inflated, so that I can later on get hold of them with findViewById(...).


0 comments:

Post a Comment