I have a custom view (which extends GLSurfaceView) I’m trying to use in my app. The xml layout that uses it appears to be being loaded correctly (attributes I set for the view are applied), but neither the view’s constructor nor the onFinishInflate method are being called for the class. I should also add that I’m new to the android platform so this is probably a stupid mistake.
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
#<!-- Status Bar -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/status_bkg">
#<!-- Location label -->
<TextView
android:text="Location"
android:textColor="#FFFFFF"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
#<!-- Date/Time label -->
<TextView
android:text="Date and Time"
android:textColor="#FFFFFF"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
#<!-- ChartView -->
<View class="com.xxx.yyy.ChartView" android:id="@+id/chartView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1">
</View>
The ChartView is the custom GLSurfaceView. This layout is loaded and the ChartView has a green background as specified. However, none of my setup (in code) happens for it. I’ve tried placing it in the two constructors and in the onFinishInflate methods, but they inexplicably don’t get called. I’ve verified that the class name is correct (the xxx.yyy are just to hide the company in this post.
I’ve also tried a different form of the xml reference
<com.xxx.yyy.ChartView android:id="@+id/chartView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"/>
But this dies when loading the Activity says that the constructor
ChartView(Context context, AttributeSet attrs)
doesn’t exist (although it is certainly in my code).
Anyone got any tips on what could be going on?
I finally figured this out. I had defined my constructor (the one not getting called) as:
However, it started working when I changed the constructor to be:
Given that MyActivity extends Activity (which extends Context), this is perfectly valid code and I believe it should work (it certainly compiles). It would seem there is an error in the reflection the system is using in locating this method.