2012-07-27 66 views
2

我想创建一个涉及按下一个按钮拍摄照片的Android应用程序。我的代码是干净的,没有任何警告或错误,但是当我在模拟器上运行它时,只要按下应用程序中的照片按钮,它就会说您的应用程序已停止。当我检查日志文件以获取解释时,我以下Android应用程序 - 没有这样的方法例外

任何帮助,将不胜感激。先谢谢你。

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

} 

回答

0

创建中调用该方法

private static final int TAKE_PHOTO_REQUEST = 100; 

,并调用方法

dispatchTakePhotoIntent(TAKE_PHOTO_REQUEST); 

和你的方法测试中的请求代码的类中的常数:

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

问题在于你通过传递给图像的视图来调用方法排除/显示,而不是你想要携带的动作。因此,编译器找到了调用dispatchTakePhotoIntent(View)的方法,因为您已定义dispatchTakePhotoIntent(int),因此无法在代码中找到该方法。

如果你在eclipse中开发,你应该在你的代码中得到一个错误(错误所在行上的红色十字)。

1

错误说:无法找到一个方法dispatchTakePhotoIntent(查看

你用视图对象作为参数调用dispatchTakePhotoIntent而在你的代码中你已经定义了:

public void dispatchTakePhotoIntent(int actionCode) 

以整数作为参数。

在代码中调用方法时存在问题。

+0

感谢您的回复,这确实解释了什么是问题,但是,我是新来的android请你解释如何发送一个整数而不是视图dispatchTakePhotoIntent(int actionCode)?再次感谢提前。 – 2012-07-27 22:22:48

+0

嘿,我一直在线,一直在给你发消息,但你退出了。我仍然在线 – 2012-08-22 18:13:25

+0

我其实看到这个答案是最直接的。我有同样的问题,并意识到这是因为我错过了我的View元素被传递给一个无参数函数。 Upvoted。 – dudewad 2012-10-12 20:23:31

0

找不到方法dispatchTakePhotoIntent(查看)在活动课com.example.mydressingroom.MainActivity为的onClick处理程序上的视图类android.widget.Button

看来你可能会被发送给浏览该方法不是像在method属性中定义的那样发送整数。

+0

感谢您的回复,这确实解释了什么是问题,但是,我是新来的android请你解释如何发送一个整数而不是视图dispatchTakePhotoIntent(int actionCode)?再次感谢提前。 – 2012-07-27 22:21:46

相关问题