当前位置: 首页 > 知识库问答 >
问题:

JAVAlang.InstantiationException:类com。实例照相机MainActivity没有零参数构造函数

司马狐若
2023-03-14
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

公共类MainActivity扩展了SurfaceView实现了SurfaceHolder。回调,预览回调{

    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);  
              }  
            }  
          }  
}  

在运行此代码时,我收到了一个错误,java.lang.InstantiationExctive:类com.example.camera.MainActive没有零参数构造函数...

共有2个答案

左华灿
2023-03-14

我扩展了活动(只是普通的旧活动),然后去掉了上面提到的所有构造函数。这使得它工作起来。在我的例子中,我从一个非活动类调用一个活动。

丌官凯康
2023-03-14

摆脱你的main活动(上下文、摄像头)constructor。没有代码会使用它。把代码移到别的地方。

然后,去掉MainActivity()构造函数,只继承超类的构造函数。

 类似资料:
  • 我正在尝试使用recyclerview和room库创建一个简单的ToDoList应用程序。在使用room和mvvm架构方面,我正在遵循android开发者代码实验室,我似乎遇到了困难。我已经设置了应用程序的每一层,但在尝试使用ViewModelProvider实例化ViewModel时出现了一个错误。下面是我的ViewModel类中的代码。 } 下面是我试图初始化ViewModel的main片段中

  • 我试图在谷歌地图中显示一个位置地址,但当我为获取地址启动意向服务时,它将显示错误“无法实例化服务”。 我使用此链接显示位置地址。 这是我的代码: 在这里我开始意图服务:

  • 注:在标记为副本之前,请仔细阅读。我已经尝试了所有现有的答案。 我在用匕首2。 视图模型实例已在活动中成功创建,但当我从同一活动中打开BottomSheet对话框时,无法创建实例。 日志: 用户界面。搜索viewmodel。SearchViewModel

  • 我通过本文档了解LiveData和ViewModel。在文档中,ViewModel类具有构造函数, 但是,当我运行代码时,会出现异常: 原因:java.lang.运行时异常:无法创建类的实例UserViewModel原因:java.lang.实例异常:java.lang.类没有零参数构造函数

  • 我是新的Android和Java,并试图使基于位置的应用程序。 编辑 我做了一个非常非常简单的测试代码,得到了同样的错误。这是java: 我也犯了同样的错误。以下是我的应用程序级构建中的依赖项。格雷德尔: 原帖 我试图使用ViewModel和LiveData来更新用户位置,因为我知道这是生命周期感知的最佳方式。我有一个默认的地图活动... 一个扩展LiveData以存储用户位置的类。。。 以及一个

  • 我正在使用并试图将我的导入我的类。 当使用ViewModel查看Google的hilt文档时,我们可以看到他们能够将注入到所述