简体   繁体   English

Android应用程序-没有此类方法异常

[英]Android app- No such method exception

I am trying to create an android app that involves pressing a button that takes a picture. 我正在尝试创建一个Android应用,其中涉及按下拍照按钮。 My code is clean without any warnings or errors but when I run it on the emulator, and as soon as I press the photo button in the app, it says that your app has stopped.When I check the log file for the explanation it gives me the following 我的代码是干净的,没有任何警告或错误,但是当我在模拟器上运行它时,当我按下应用程序中的照片按钮时,它表示您的应用程序已停止。当我查看日志文件以获取说明时我以下

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you in advance. 先感谢您。

    07-27 20:44:11.676: E/AndroidRuntime(453): FATAL EXCEPTION: main
    07-27 20:44:11.676: E/AndroidRuntime(453): java.lang.IllegalStateException: Could not find a method dispatchTakePhotoIntent(View) in the activity class com.example.mydressingroom.MainActivity for onClick handler on view class android.widget.Button
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.view.View$1.onClick(View.java:3026)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.view.View.performClick(View.java:3480)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.view.View$PerformClick.run(View.java:13983)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.os.Handler.handleCallback(Handler.java:605)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.os.Handler.dispatchMessage(Handler.java:92)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.os.Looper.loop(Looper.java:137)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.app.ActivityThread.main(ActivityThread.java:4340)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at java.lang.reflect.Method.invoke(Method.java:511)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at dalvik.system.NativeStart.main(Native Method)
    07-27 20:44:11.676: E/AndroidRuntime(453): Caused by: java.lang.NoSuchMethodException: dispatchTakePhotoIntent [class android.view.View]
    07-27 20:44:11.676: E/AndroidRuntime(453):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at java.lang.Class.getMethod(Class.java:915)
    07-27 20:44:11.676: E/AndroidRuntime(453):  at android.view.View$1.onClick(View.java:3019)
    07-27 20:44:11.676: E/AndroidRuntime(453):  ... 11 more

Here is my code 这是我的代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    }


    /** Called when the user clicks the Photo button */
    public void dispatchTakePhotoIntent(int actionCode) {
          Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(takePhotoIntent, actionCode);
    }

}

the error says: could not find a method dispatchTakePhotoIntent( View ) 错误提示:找不到方法dispatchTakePhotoIntent( View

You a calling dispatchTakePhotoIntent with a View object as an argument to it whereas in your code you have defined: 您以View对象作为参数的调用dispatchTakePhotoIntent,而在代码中已定义:

public void dispatchTakePhotoIntent(int actionCode) 

which takes an integer as an argument. 它以整数作为参数。

There is a problem in the way you are calling your method inside your code. 您在代码内调用方法的方式存在问题。

Could not find a method dispatchTakePhotoIntent(View) in the activity class com.example.mydressingroom.MainActivity for onClick handler on view class android.widget.Button 在视图类android.widget.Button的onClick处理程序的活动类com.example.mydressingroom.MainActivity中找不到方法dispatchTakePhotoIntent(View)

It seems you might be sending a View to the method, instead of sending a integer as you have defined in the method attribute. 看来您可能正在向该方法发送一个View,而不是像在method属性中定义的那样发送整数。

create a constant in the class which calls the method 在调用方法的类中创建一个常量

  private static final int TAKE_PHOTO_REQUEST = 100;

and call the method as 并将方法调用为

  dispatchTakePhotoIntent(TAKE_PHOTO_REQUEST);

and inside your method test for the request code: 并在您的方法测试中查找请求代码:

  public void dispatchTakePhotoIntent(int actionCode) {
    if (actionCode == 100) {
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePhotoIntent, actionCode);
    }
  }

and the problem is that you are calling the method by passing to it the view in which the image is being rendered/displayed rather than the action which you want to carry. 问题是您通过将要在其中渲染/显示图像的视图(而不是要执行的操作)传递给该方法来调用该方法。 So the compiler found a call to the method of type dispatchTakePhotoIntent(View) which it could not find in your code since you have defined dispatchTakePhotoIntent(int) . 因此,编译器找到了对dispatchTakePhotoIntent(View)类型的方法的调用,该调用在您的代码中找不到,因为您已经定义了dispatchTakePhotoIntent(int)

if you are developing in eclipse you should be getting an error in your code (a red cross on the line where the error is). 如果您正在使用Eclipse进行开发,则应该在代码中出现错误(错误所在行上的红叉)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM