Android应用程序-没有此类方法异常 [英] Android app- No such method exception

查看:90
本文介绍了Android应用程序-没有此类方法异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Android应用,其中涉及按下拍照按钮.我的代码是干净的,没有任何警告或错误,但是当我在模拟器上运行它时,当我按下应用程序中的照片按钮时,它表示您的应用程序已停止.当我查看日志文件以获取说明时我以下

I am trying to create an android app that involves pressing a button that takes a picture. 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

这是我的代码

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

}

推荐答案

在调用该方法的类中创建一个常量

create a constant in the class which calls the method

  private static final int TAKE_PHOTO_REQUEST = 100;

并将方法调用为

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

,问题在于您通过将在其中渲染/显示图像的视图(而不是要执行的操作)传递给该方法来调用该方法.因此,编译器找到了对类型为 dispatchTakePhotoIntent(View)的方法的调用,由于定义了 dispatchTakePhotoIntent(int),该调用在代码中找不到.

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).

如果您正在使用eclipse进行开发,则应该在代码中出现错误(错误所在行上的红叉).

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).

这篇关于Android应用程序-没有此类方法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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