java.lang.InstantiationException:类com.example.camera.MainActivity没有零参数的构造函数 [英] java.lang.InstantiationException: class com.example.camera.MainActivity has no zero argument constructor

查看:1007
本文介绍了java.lang.InstantiationException:类com.example.camera.MainActivity没有零参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

\r
\r

12月三日日至2日:03:23.564:D / AndroidRuntime(9302):关闭VM下来\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):致命异常:主要\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):工艺:com.example.camera,PID:9302\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):了java.lang.RuntimeException:无法实例活动ComponentInfo {} com.example.camera/com.example.camera.MainActivity:java.lang.InstantiationException:类com.example.camera.MainActivity没有零参数的构造函数\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.app.ActivityThread.access $ 800(ActivityThread.java:148)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.os.Handler.dispatchMessage(Handler.java:102)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.os.Looper.loop(Looper.java:135)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.app.ActivityThread.main(ActivityThread.java:5312)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在java.lang.reflect.Method.invoke(本机方法)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在java.lang.reflect.Method.invoke(Method.java:372)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):java.lang.InstantiationException:致类com.example.camera.MainActivity没有零参数的构造函数\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在java.lang.Class.newInstance(Class.java:1563)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.app.Instrumentation.newActivity(Instrumentation.java:1088)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):10 ...更多\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):java.lang.NoSuchMethodException:产生的原因<&初始化GT; []\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在java.lang.Class.getConstructor(Class.java:531)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在java.lang.Class.getDeclaredConstructor(Class.java:510)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):在java.lang.Class.newInstance(Class.java:1561)\r
12月3日至二日:03:23.570:E / AndroidRuntime(9302):12 ...更多

\r

\r
\r

公共类MainActivity扩展SurfaceView实现SurfaceHolder.Callback,$ P $ {pviewCallback

  SurfaceHolder mHolder;
    相机mCamera;
 //这个变量负责获取和设置相机设置
    私人参数参数;
    //这个变量存储摄像机preVIEW大小
    私人尺寸previewSize;
    //这个数组存储像素为十六进制对
    私人诠释[]像素;
    公共MainActivity(上下文的背景下,相机摄像头){
    超级(上下文);
    // TODO自动生成构造函数存根
    mCamera =摄像头;
      //安装SurfaceHolder.Callback所以我们得到通知时,
    //创建下垫面和销毁。
    mHolder = getHolder();
    mHolder.addCallback(本);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    新MainActivity(){};
}    公共MainActivity(){
        // TODO自动生成构造函数存根
    }    公共无效surfaceCreated(SurfaceHolder持有人){
        //表面有被创建,获取摄像机,并告诉它在哪里
        //绘制。
        如果(mCamera!= NULL)
        {
        mCamera = Camera.open();
        }
        尝试{
           mCamera.set previewDisplay(支架);           //设置要的人在这个类中定义的相机回调
           mCamera.set previewCallback(本);
           mCamera.start preVIEW();
           ///初始化变量
           参数= mCamera.getParameters();
           previewSize = parameters.get previewSize();
           像素=新INT [previewSize.width * previewSize.height]。        }赶上(IOException异常除外){
            mCamera.release();
            mCamera = NULL;
            // TODO:在此处增加更多的异常处理逻辑
        }
    }    公共无效surfaceDestroyed(SurfaceHolder持有人){
        //当我们回到地面就会被破坏,因此停止preVIEW。
        //因为CameraDevice对象不是共享资源,这是非常
        //重要的是释放它当活动被暂停。
        mCamera.stop preVIEW();
        mCamera.release();
        mCamera = NULL;
    }    公共无效surfaceChanged(SurfaceHolder架,INT格式,诠释W,INT高){
        //现在,尺寸是已知的,设置相机参数,并开始
        //将preVIEW。
        parameters.set previewSize(W,H);
        //设置相机的设置
        mCamera.setParameters(参数);
        mCamera.start preVIEW();
    }    @覆盖
    在previewFrame(字节[]数据,相机摄像头){公共无效
        //变换NV21像素数据转换成RGB像素
        德codeYUV420SP(像素,数据,previewSize.width,previewSize.height);
        // Outuput左上像素的在preVIEW到LogCat中的值
        Log.i(像素,右上像素具有以下的RGB(十六进制)的值:
                + Integer.toHexString(像素[0]));
    }    //从科泰工程方法!不是我的!见下文...
    虚空德codeYUV420SP(INT [] RGB,字节[] yuv420sp,诠释的宽度,高度INT){            最终诠释框架尺寸=宽*高;            为(诠释J = 0,YP = 0; J&下;高度; J ++){INT UVP =框架尺寸+(J>→1)*宽度,U = 0,V = 0;
              的for(int i = 0; I<宽度;我++,YP ++){
                INT Y =(0xFF的及((int)的yuv420sp [YP])) - 16;
                如果(γ℃的)
                  Y = 0;
                如果((I和1)== 0){
                  V =(0xFF的&安培; yuv420sp [UVP ++]) - 128;
                  U =(0xFF的&安培; yuv420sp [UVP ++]) - 128;
                }                INT y1192 = 1192 * Y;
                INT R =(y1192 + 1634 * V);
                INT G =(y1192 - 833 * V - 400 * U);
                INT B =(y1192 + 2066 * U);                如果(为r 0)R = 0;否则如果(R> 262143)
                   R = 262143;
                如果(克小于0)G = 0;否则如果(g取代; 262143)
                   G = 262143;
                如果(二℃,)B = 0;否则,如果(B> 262143)
                   B = 262143;                RGB [YP] = 0xff000000 | ((为r 6;)及为0xFF0000)| ((g取代;→2)及为0xFF00)| ((B个大于10)及0xff的);
              }
            }
          }
}

在运行此code我收到一个错误,说明java.lang.InstantiationException:类com.example.camera.MainActivity没有零参数的构造函数...


解决方案

摆脱你的 MainActivity(上下文的背景下,摄像头摄像头)的构造函数。没有code将永远使用它。将其在code别处。

然后,甩掉你的 MainActivity()构造,只是继承父类的构造函数。

03-02 12:03:23.564: D/AndroidRuntime(9302): Shutting down VM
03-02 12:03:23.570: E/AndroidRuntime(9302): FATAL EXCEPTION: main
03-02 12:03:23.570: E/AndroidRuntime(9302): Process: com.example.camera, PID: 9302
03-02 12:03:23.570: E/AndroidRuntime(9302): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.camera/com.example.camera.MainActivity}: java.lang.InstantiationException: class com.example.camera.MainActivity has no zero argument constructor
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.app.ActivityThread.access$800(ActivityThread.java:148)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.os.Looper.loop(Looper.java:135)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.app.ActivityThread.main(ActivityThread.java:5312)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at java.lang.reflect.Method.invoke(Native Method)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at java.lang.reflect.Method.invoke(Method.java:372)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-02 12:03:23.570: E/AndroidRuntime(9302): Caused by: java.lang.InstantiationException: class com.example.camera.MainActivity has no zero argument constructor
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at java.lang.Class.newInstance(Class.java:1563)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.app.Instrumentation.newActivity(Instrumentation.java:1088)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	... 10 more
03-02 12:03:23.570: E/AndroidRuntime(9302): Caused by: java.lang.NoSuchMethodException: <init> []
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at java.lang.Class.getConstructor(Class.java:531)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at java.lang.Class.getDeclaredConstructor(Class.java:510)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	at java.lang.Class.newInstance(Class.java:1561)
03-02 12:03:23.570: E/AndroidRuntime(9302): 	... 12 more

public class MainActivity extends SurfaceView implements SurfaceHolder.Callback, PreviewCallback {

    SurfaceHolder mHolder;  
    Camera mCamera;  
 //This variable is responsible for getting and setting the camera settings  
    private Parameters parameters;  
    //this variable stores the camera preview size   
    private Size previewSize;  
    //this array stores the pixels as hexadecimal pairs   
    private int[] pixels;  


    public MainActivity(Context context, Camera camera) {
    super(context);
    // TODO Auto-generated constructor stub
    mCamera = camera;
      // Install a SurfaceHolder.Callback so we get notified when the  
    // underlying surface is created and destroyed.  
    mHolder = getHolder();  
    mHolder.addCallback(this);  
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    new MainActivity(){};
}

    public MainActivity() {
        // TODO Auto-generated constructor stub
    }

    public void surfaceCreated(SurfaceHolder holder) {  
        // The Surface has been created, acquire the camera and tell it where  
        // to draw.  
        if (mCamera != null)
        {
        mCamera = Camera.open();
        }
        try {  
           mCamera.setPreviewDisplay(holder);  

           //sets the camera callback to be the one defined in this class  
           mCamera.setPreviewCallback(this);  
           mCamera.startPreview();
           ///initialize the variables  
           parameters = mCamera.getParameters();  
           previewSize = parameters.getPreviewSize();  
           pixels = new int[previewSize.width * previewSize.height];  

        } catch (IOException exception) {  
            mCamera.release();  
            mCamera = null;  
            // TODO: add more exception handling logic here  
        }  
    }  

    public void surfaceDestroyed(SurfaceHolder holder) {  
        // Surface will be destroyed when we return, so stop the preview.  
        // Because the CameraDevice object is not a shared resource, it's very  
        // important to release it when the activity is paused.  
        mCamera.stopPreview();  
        mCamera.release();  
        mCamera = null;  
    }  

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {  
        // Now that the size is known, set up the camera parameters and begin  
        // the preview.  
        parameters.setPreviewSize(w, h);  
        //set the camera's settings  
        mCamera.setParameters(parameters);  
        mCamera.startPreview();  
    }  

    @Override  
    public void onPreviewFrame(byte[] data, Camera camera) {  
        //transforms NV21 pixel data into RGB pixels  
        decodeYUV420SP(pixels, data, previewSize.width,  previewSize.height);  
        //Outuput the value of the top left pixel in the preview to LogCat  
        Log.i("Pixels", "The top right pixel has the following RGB (hexadecimal) values:"  
                +Integer.toHexString(pixels[0]));     
    }  

    //Method from Ketai project! Not mine! See below...  
    void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {  

            final int frameSize = width * height;  

            for (int j = 0, yp = 0; j < height; j++) {       int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;  
              for (int i = 0; i < width; i++, yp++) {  
                int y = (0xff & ((int) yuv420sp[yp])) - 16;  
                if (y < 0)  
                  y = 0;  
                if ((i & 1) == 0) {  
                  v = (0xff & yuv420sp[uvp++]) - 128;  
                  u = (0xff & yuv420sp[uvp++]) - 128;  
                }  

                int y1192 = 1192 * y;  
                int r = (y1192 + 1634 * v);  
                int g = (y1192 - 833 * v - 400 * u);  
                int b = (y1192 + 2066 * u);  

                if (r < 0)                  r = 0;               else if (r > 262143)  
                   r = 262143;  
                if (g < 0)                  g = 0;               else if (g > 262143)  
                   g = 262143;  
                if (b < 0)                  b = 0;               else if (b > 262143)  
                   b = 262143;  

                rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);  
              }  
            }  
          }  
}  

While running this code i received an error stating java.lang.InstantiationException: class com.example.camera.MainActivity has no zero argument constructor...

解决方案

Get rid of your MainActivity(Context context, Camera camera) constructor. No code will ever use it. Move its code somewhere else.

Then, get rid of your MainActivity() constructor, and just inherit the superclass' constructor.

这篇关于java.lang.InstantiationException:类com.example.camera.MainActivity没有零参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆