Question

I have some code that works perfect with no errors if the view class (JavaCameraImageView) is inflated in the XML layout as an external class, it also works if the class is included in the same .java file external to the main activity class. however when I tried to use the JavaCameraImageView class as an inner class inside of the main class I get the errors and logcat output shown here. how do I get this to work as nested inner class?

Logcat result

 01-14 15:17:37.486: E/AndroidRuntime(8347): FATAL EXCEPTION: main
 01-14 15:17:37.486: E/AndroidRuntime(8347): java.lang.RuntimeException:
 Unable to start activity ComponentInfo{com.group.cam/com.group.cam.MainActivity}:
 android.view.InflateException: Binary XML file line #28: Error inflating class
 com.group.cam.MainActivity$JavaCameraImageView

XML layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity" >

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

 <view
  class="com.group.cam.MainActivity$JavaCameraImageView"
  android:id="@+id/camera_surface_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
 />       

</FrameLayout>

</LinearLayout>

onCreate method of class MainActivity

        @Override
    public void onCreate(Bundle savedInstanceState) {

        if (!OpenCVLoader.initDebug()) {
            // Handle initialization error
        }

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        frameLayoutOne = (FrameLayout) findViewById(R.id.frameLayout1);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        mOpenCvCameraView = (JavaCameraImageView) findViewById(R.id.camera_surface_view);
        mOpenCvCameraView.setCvCameraViewListener(this);


        mOpenCvCameraView.setMaxFrameSize(reducedDimensions.width, reducedDimensions.height);

    } // end on create

class JavaCameraImageView that nested as an inner class inside of MainActivity class

   public class JavaCameraImageView extends JavaCameraView {

 public JavaCameraImageView(Context context, AttributeSet attrs) {
      super(context, attrs);
       // TODO Auto-generated constructor stub
 }      
}

EDIT: solution to this problem, the JavaCameraImageView class must be made static for it to work as an inner class in this situation

   public static class JavaCameraImageView extends JavaCameraView {

 public JavaCameraImageView(Context context, AttributeSet attrs) {
      super(context, attrs);
       // TODO Auto-generated constructor stub
 }      
}
Was it helpful?

Solution

error is because of this

 <view
  class="com.group.cam.MainActivity$JavaCameraImageView"
  android:id="@+id/camera_surface_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent" 
 />       

you should make your inner class static.

The thing is static inner classes are nothing but a class which is accessible via it's outer class.

This class does not belong to the instance of the outer class but the outer class itself so to create an object of static inner class all you need to do is:

new Outer.Inner();

Outer.Inner becomes it's qualified name

But if your inner class is not static then it belongs to the instance of the outer class so to create an object you need to write:

new Outer().new Inner();

This class is not accessible view Outer class but via Object of outer class.

That is why when you use it in manifest this class is not accessible via your class name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top