0

I am trying to display the camera view using a class extends surfaceview and implements surfaceholder.callback. in my main activity ViewActivity, as shown below, i instantiate an object from the SurfaceHolderActivity and set it as a view using setContentView(myCameraSurfaceHolder);. while running the app on the device, it crashes and logcat gives an error sayin no empty constructor. please help me to find the error.

ViewActivity:

private SurfaceHolderActivity myCameraSurfaceHolder;
    private android.hardware.Camera myCamera;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
        requestWindowFeature( Window.FEATURE_NO_TITLE );
        getWindow().setFormat(PixelFormat.TRANSPARENT);
        getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN );

        myCameraSurfaceHolder = new SurfaceHolderActivity(this);
        setContentView(myCameraSurfaceHolder);
    }

SurfaceHolderActivity:

public SurfaceHolderActivity(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    this.mySurfaceHorder = this.getHolder();
    this.mySurfaceHorder.addCallback(this);
    //this.mySurfaceHorder.setType(mySurfaceHorder.SURFACE_TYPE_PUSH_BUFFERS);
    //this.mySurfaceHorder.setFormat(mySurfaceHorder.setFormat(PixelFormat.TRANSLUCENT));
}

logcat:

04-24 04:34:07.406: E/AndroidRuntime(21420): FATAL EXCEPTION: main
04-24 04:34:07.406: E/AndroidRuntime(21420): java.lang.RuntimeException: Unable to 
instantiate activity    omponentInfo{com.example.augrealtest00/com.example.augrealtest00.SurfaceHolderActivity}: 
java.lang.InstantiationException: can't instantiate class 
 com.example.augrealtest00.SurfaceHolderActivity; no empty constructor

4 Answers4

0

did you try this in your SurfaceHolderActivity:

public SurfaceHolderActivity(Context context) { super(context); // TODO Auto-generated constructor stub this.mySurfaceHorder = this.getHolder(); this.mySurfaceHorder.addCallback( context ); //this.mySurfaceHorder.setType(mySurfaceHorder.SURFACE_TYPE_PUSH_BUFFERS); //this.mySurfaceHorder.setFormat(mySurfaceHorder.setFormat(PixelFormat.TRANSLUCENT)); }

CompEng
  • 7,161
  • 16
  • 68
  • 122
0

You cannot instantiate an Activity directly! You never construct an activity with a constructor, so you can't use the constructor to pass in parameters. Please visit http://developer.android.com/guide/components/activities.html#Creating to find how to create an Activity.

makovkastar
  • 5,000
  • 2
  • 30
  • 50
0

Don't make your SurfaceView extends Activity--you should extends SurfaceView, NOT Activity!

The logcat messages show that you are trying to create a class extends Activity, but you didnt provide default constructor(constructor with no arguments). So the Android framework cannot create your activity!.

You should modify your SurfaceholderActivity class and extends SurfaceView instead of Activity. Possibly Modify AndroidManifest.xml if you have add SurfaceHolderAcitivty in activity tag.

Try to modify your code like this:

public class SurfaceHolderActivity extends SurfaceView implements Callback {

    private SurfaceHolder mySerfaceHolder;

    public SurfaceHolderActivity(Context context) {
        super(context);
        this.mySerfaceHolder = this.getHolder();
        this.mySerfaceHolder.addCallback(this);     
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO some implementation...
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO some implementation...
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO some implementation...
    }
}

And I suggest you to rename SurfaceHolderActivity as MySurfaceView since it should be a View but not an Activity.

chartsai
  • 368
  • 3
  • 7
  • my surface view extends a view not an activity, exactly as you suggested –  Apr 24 '14 at 20:25
  • I am confused about "this.getHolder()" in SurfaceHolderActivity, this function is exist in the surfaceview but not activity. Maybe you can update your customize surfaceview code? – chartsai Apr 26 '14 at 16:30
0

My mistake was that, in the manifest file the class SurfaceView was placed as the main activity while it is not an activity it is just a class. I just modified the manifest file in the way that the ViewActivity is my main activity. and it works fine. Thanks to all who provided help.