2 vampirecoding vampirecoding 于 2016.04.02 21:24 提问

Could not execute method for android :onClicked

最近在学android的camera,在拍照按钮的点击事件中做了如下参数设置:

     public void capture(View view){
        if(mCamera!=null){
            WindowManager manager= (WindowManager) getSystemService(Context.WINDOW_SERVICE);
            Display display=manager.getDefaultDisplay();
            Log.i("capture",display.getWidth()+" "+display.getHeight());

            Camera.Parameters parameters=mCamera.getParameters();

            //设置拍照格式
            parameters.setPictureFormat(PixelFormat.YCbCr_422_SP);
            //设置拍照大小
            parameters.setPictureSize(213,350);
            //设置自动对焦
            parameters.setFlashMode(Camera.Parameters.FOCUS_MODE_AUTO);

            mCamera.setParameters(parameters);


        mCamera.autoFocus(new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                mCamera.takePicture(null,null,mPictureCallback);
            }
        });
        }
    }

点击拍照按钮的时候程序崩溃了,出现如下的日志信息:

                                                                                  Process: com.example.vampire.facelearning, PID: 14489
                                                                                  java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4461)
                                                                                      at android.view.View.performClick(View.java:5207)
                                                                                      at android.view.View$PerformClick.run(View.java:21177)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5432)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
                                                                                   Caused by: java.lang.reflect.InvocationTargetException
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4456)
                                                                                      at android.view.View.performClick(View.java:5207) 
                                                                                      at android.view.View$PerformClick.run(View.java:21177) 
                                                                                      at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5432) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
                                                                                   Caused by: java.lang.RuntimeException: setParameters failed
                                                                                      at android.hardware.Camera.native_setParameters(Native Method)
                                                                                      at android.hardware.Camera.setParameters(Camera.java:2003)
                                                                                      at com.example.vampire.facelearning.CustomCamera.capture(CustomCamera.java:85)
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4456) 
                                                                                      at android.view.View.performClick(View.java:5207) 
                                                                                      at android.view.View$PerformClick.run(View.java:21177) 
                                                                                      at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5432) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 

我把mCamera.setParameters(parameters);注析掉后程序就可以进行拍照了,但遇到了另一个问题。拍照获取的图片尺寸非常小:图片说明

这是onPictureToken方法的实现方法:

        private Camera.PictureCallback mPictureCallback=new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            //将数据写入SD卡的temp.png文件当中
            File tempFile=new File("/sdcard/temp.png");
            try {
                FileOutputStream fileOutputStream=new FileOutputStream(tempFile);
                fileOutputStream.write(data);
                fileOutputStream.close();

                Intent intent=new Intent(CustomCamera.this,ResultAty.class);
                intent.putExtra("picPath",tempFile.getAbsolutePath());
                startActivity(intent);
                CustomCamera.this.finish();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

跳转到另一个activity来进行显示图片:

    String path=getIntent().getStringExtra("picPath");
        ImageView imageView= (ImageView) findViewById(R.id.pic);

        //对图像的显示角度进行调整
        try {
            FileInputStream fis=new FileInputStream(path);
            //把图像转换成字节流
            Bitmap bitmap=BitmapFactory.decodeStream(fis);
            Matrix matrix=new Matrix();
            matrix.setRotate(90);
            //调整角度
            bitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
            imageView.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } 

麻烦各位大神给个提示,我自己debug了很久都没有找出问题所在,谢谢!

2个回答

CSDNXIAOC
CSDNXIAOC   2016.04.02 21:32
已采纳

今天在学习Android跳转Activity的时候,发现视频里可以直接startActivity()......
答案就在这里:解决Could not execute method of the activity
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

devmiao
devmiao   Ds   Rxr 2016.04.03 03:59
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!