I think I figured out why this wasn't working. I was only providing a constructor for the case of one parameter 'context' when I should have provided a constructor for the two parameter 'Context, AttributeSet' case. I also needed to give the constructor(s) public access. Here's my fix:
public class GhostSurfaceCameraView extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
public GhostSurfaceCameraView(Context context)
{
super(context);
init();
}
public GhostSurfaceCameraView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public GhostSurfaceCameraView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Tim - Both the constructors are not required, only the ViewClassName(Context context, AttributeSet attrs )
constructor is necessary. I found this out the painful way, after hours and hours of wasted time.
I am very new to Android development, but I am making a wild guess here, that it maybe due to the fact that since we are adding the custom View
class in the XML file, we are setting several attributes to it in the XML, which needs to be processed at the time of instantiation. Someone far more knowledgeable than me will be able to shed clearer light on this matter though.
Another possible cause of the "Error inflating class" message could be misspelling the full package name where it's specified in XML:
<com.alpenglow.androcap.GhostSurfaceCameraView android:id="@+id/ghostview_cameraview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Opening your layout XML file in the Eclipse XML editor should highlight this problem.
It's important to write full class path in the xml. I got 'Error inflating class' when only subclass's name was written in.
I had this error plaguing me for the past few hours. Turns out, I had added the custom view lib as a module in Android Studio, but I had neglected to add it as a dependency in app's build.gradle
.
dependencies {
...
compile project(':gifview')
}
fwiw, I received this error due to some custom initialization within the constructor attempting to access a null object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With