简体   繁体   中英

Android app- No such method exception

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

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 )

You a calling dispatchTakePhotoIntent with a View object as an argument to it whereas in your code you have defined:

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

It seems you might be sending a View to the method, instead of sending a integer as you have defined in the method attribute.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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