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币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
解决Could not execute method of the activity
Android编程里面很多问题其实都不算大问题,很多错误也是小错误,比如这次要说的 LogCat信息: java.lang.IllegalStateException: Could not execute method of the activity Caused by: java.lang.reflect.InvocationTargetException Caused by:
新增activity报错,Could not execute method of the activity
新增activity报错,Could not execute method of the activity 新增Activity,必须在AndroidManifest.xml中配置,不然startActivity(intent);时会崩溃。
java.lang.IllegalStateException: Could not execute method of the activity
07-03 13:29:25.616: D/gralloc_goldfish(1888): Emulator without GPU emulation detected. 07-03 13:29:27.956: W/KeyCharacterMap(1888): No keyboard for id 0 07-03 13:29:27.956: W/KeyCharacterMap(1888):
越过Android中布局文件中使用onClick属性的坑
最近在使用onClick属性时,发现总是崩溃,没办法,回归本源这个时候就去翻官方文档,希望从中找到答案。先来看看官方文档是怎么描述的吧: Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method tha
android源码解析(1)--如何处理布局文件中添加的点击事件
我们都知道给view设置点击事件有三种方式:          第一种:View.setOnClickLintener(new OnClicklistener(...));          第二种:View.setOnClickListener(this); 然后让class去实现点击事件          第三种:xml文件中写onClick="XXXX";然后在activity中写方法
当android编译出现com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536错误时的处理
当我们在一个项目写的代码越来越多时,在编译项目时就可能出现如下错误(工具不同,版本不同,提示可能有差异): UNEXPECTEDTOP-LEVEL EXCEPTION: com.android.dex.DexIndexOverflowException:method ID not in [0, 0xffff]: 65536 atcom.android.dx.merge.Dex
Android开发当中遇到的各种error以及解决方法
2013-4-3 09:56:16: Could not find class 'com.[packageName].NdComPlatformHandler$1', referenced from method com.[packageName].NdComPlatformHandler. 91SDK的Library Project应用部正确引起的。 关于library project的解
hibernate框架时遇到的错误ognl.MethodFailedException: Method "execute" f
ognl.MethodFailedException: Method "execute" failed for object com.toy.action.Test@150c25 [java.lang.NoClassDefFoundError: org/apache/lucene/analysis/standard/StandardAnalyzer]     ognl.OgnlRuntime
java.lang.IllegalStateException: Could not find a method capture(View) in the activity class
java.lang.IllegalStateException: Could not find a method capture(View) in the activity class com.android.xiong.cameratest.MainActivity for onClick handler on view class android.widget.Button with id '
android Unable toexecute dex: method ID not in [0, 0xffff]: 65536问题
尊重原创 :http://blog.csdn.net/yuanzeyao/article/details/41809423 作为一名Android开发者,相信你对Android方法数不能超过65K的限制应该有所耳闻,随着应用程序功能不断的丰富,总有一天你会遇到一个异常: Conversion to Dalvik format failed:Unable toexecute dex: