[英]Android app- No such method exception
我正在嘗試創建一個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);
}
}
錯誤提示:找不到方法dispatchTakePhotoIntent( View )
您以View對象作為參數的調用dispatchTakePhotoIntent,而在代碼中已定義:
public void dispatchTakePhotoIntent(int actionCode)
它以整數作為參數。
您在代碼內調用方法的方式存在問題。
在視圖類android.widget.Button的onClick處理程序的活動類com.example.mydressingroom.MainActivity中找不到方法dispatchTakePhotoIntent(View)
看來您可能正在向該方法發送一個View,而不是像在method屬性中定義的那樣發送整數。
在調用方法的類中創建一個常量
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進行開發,則應該在代碼中出現錯誤(錯誤所在行上的紅叉)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.