编译器没有看到新方法名称 - 构建问题但是 运行 时间错误。 "java.lang.IllegalStateException: Could not find a method"

Compiler doesn't see new method name - build issue but run time error. "java.lang.IllegalStateException: Could not find a method"

我以常规方式从现有的 class 创建了一个新的 Activity class,然后从我的 ScanActivity.class 中剪切和粘贴代码,因为新的class 会非常相似。

新的 class 是 SenderActivity2.class。我还更改了 XML onClick 处理程序,使其具有 onPicButtonClicked 侦听器,而不是 ScanActivity class.

的 onScanButtonClicked

gradle 构建和项目构建一样有效,但在 运行 按钮单击时失败。

你可以在logcat中看到它还在寻找onScanButtonClicked方法。我实际上进去并将方法名称改回 onScanButtonClicked,即使该方法在另一个 class 中,看看它是否会以某种方式工作。它做了。此外,当我第一次创建新的 class 并且自动生成 XML 时,为 XML 创建的名称存在问题。我认为这可能是问题根源的重要线索。我删除了主目录中的 .gradle 文件并强制它重建所有内容,但这没有帮助。希望你们中的一些人遇到过类似的事情。我知道某处有一个文件需要重建..只是不知道它是什么或在哪里。

01-29 22:19:57.714  12606-12606/com.blissbot.hallmarktest I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
01-29 22:19:57.714  12606-12606/com.blissbot.hallmarktest I/SELinux﹕ Function: selinux_android_load_priority , loading version is VE=SEPF_SPH-L720T_4.4.2_0032
01-29 22:19:57.714  12606-12606/com.blissbot.hallmarktest I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
01-29 22:19:57.714  12606-12606/com.blissbot.hallmarktest D/dalvikvm﹕ Late-enabling CheckJNI
01-29 22:19:57.844  12606-12606/com.blissbot.hallmarktest W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-29 22:19:57.904  12606-12606/com.blissbot.hallmarktest E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
01-29 22:19:58.014  12606-12606/com.blissbot.hallmarktest I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build:  (CL3869936)
    OpenGL ES Shader Compiler Version: 17.01.11.SPL
    Build Date: 01/17/14 Fri
    Local Branch:
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
01-29 22:19:58.064  12606-12606/com.blissbot.hallmarktest D/OpenGLRenderer﹕ Enabling debug mode 0
01-29 22:20:00.066  12606-12606/com.blissbot.hallmarktest W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-29 22:20:00.086  12606-12606/com.blissbot.hallmarktest E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
01-29 22:20:00.106  12606-12606/com.blissbot.hallmarktest D/dalvikvm﹕ Trying to load lib /data/app-lib/com.blissbot.hallmarktest-114/libjmoodstocks-sdk.so 0x429498b8
01-29 22:20:00.106  12606-12606/com.blissbot.hallmarktest D/dalvikvm﹕ Added shared lib /data/app-lib/com.blissbot.hallmarktest-114/libjmoodstocks-sdk.so 0x429498b8
01-29 22:20:00.106  12606-12606/com.blissbot.hallmarktest D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.blissbot.hallmarktest-114/libjmoodstocks-sdk.so 0x429498b8, skipping init
01-29 22:20:00.106  12606-12606/com.blissbot.hallmarktest D/dalvikvm﹕ Trying to load lib /data/app-lib/com.blissbot.hallmarktest-114/libjmoodstocks-sdk-core-armv7-neon.so 0x429498b8
01-29 22:20:00.106  12606-12606/com.blissbot.hallmarktest D/dalvikvm﹕ Added shared lib /data/app-lib/com.blissbot.hallmarktest-114/libjmoodstocks-sdk-core-armv7-neon.so 0x429498b8
01-29 22:20:00.106  12606-12606/com.blissbot.hallmarktest D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.blissbot.hallmarktest-114/libjmoodstocks-sdk-core-armv7-neon.so 0x429498b8, skipping init
01-29 22:20:00.166  12606-12606/com.blissbot.hallmarktest D/Moodstocks SDK﹕ Sync will start.
01-29 22:20:00.376  12606-12606/com.blissbot.hallmarktest D/Moodstocks SDK﹕ Sync succeeded (7 images)
01-29 22:20:06.342  12606-12606/com.blissbot.hallmarktest D/AndroidRuntime﹕ Shutting down VM
01-29 22:20:06.342  12606-12606/com.blissbot.hallmarktest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41708da0)
01-29 22:20:06.352  12606-12606/com.blissbot.hallmarktest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.blissbot.hallmarktest, PID: 12606
    java.lang.IllegalStateException: Could not find a method onScanButtonClicked(View) in the activity class com.blissbot.hallmarktest.SenderActivity2 for onClick handler on view class android.widget.Button with id 'button'
            at android.view.View.onClick(View.java:3956)
            at android.view.View.performClick(View.java:4633)
            at android.view.View$PerformClick.run(View.java:19330)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: onScanButtonClicked [class android.view.View]
            at java.lang.Class.getConstructorOrMethod(Class.java:472)
            at java.lang.Class.getMethod(Class.java:857)
            at android.view.View.onClick(View.java:3949)
            at android.view.View.performClick(View.java:4633)
            at android.view.View$PerformClick.run(View.java:19330)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
01-29 22:20:08.074  13193-13193/com.blissbot.hallmarktest W/ApplicationPackageManager﹕ getCSCPackageItemText()
01-29 22:20:08.124  13193-13193/com.blissbot.hallmarktest E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
01-29 22:20:08.234  13193-13193/com.blissbot.hallmarktest I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build:  (CL3869936)
    OpenGL ES Shader Compiler Version: 17.01.11.SPL
    Build Date: 01/17/14 Fri
    Local Branch:
    Remote Branch:
    Local Patches:
    Reconstruct Branch:
01-29 22:20:08.284  13193-13193/com.blissbot.hallmarktest D/OpenGLRenderer﹕ Enabling debug mode 0

发件人Activity2 代码..

public class SenderActivity2 extends Activity implements Scanner.SyncListener {

// Moodstocks API key/secret pair
private static final String API_KEY = "xxxxxxxxxxxxxxxxxx";
private static final String API_SECRET = "xxxxxxxxxxxxxxxxxx";

private boolean compatible = false;
private Scanner scanner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipient);

    compatible = Scanner.isCompatible();
    if (compatible) {
        try {
            scanner = Scanner.get();
            String path = Scanner.pathFromFilesDir(this, "scanner.db");
            scanner.open(path, API_KEY, API_SECRET);
            scanner.setSyncListener(this);
            scanner.sync();
        } catch (MoodstocksError e) {
            e.printStackTrace();
        }
    }

}// On Create END

@Override
protected void onDestroy() {
    super.onDestroy();
    if (compatible) {
        try {
            scanner.close();
            scanner.destroy();
            scanner = null;
        } catch (MoodstocksError e) {
            e.printStackTrace();
        }
    }
}


public void onPicButtonClicked(View view) {
    if (compatible) {
        startActivity(new Intent(this, PicActivity.class));
    }
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SenderActivity2"
android:background="@drawable/hallmark_2">

<TextView
    android:focusable="false"
    android:selectable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tapcrownTextPic"
    android:textSize="18sp"
    android:text="@string/touch_crown"
    android:background="@color/white"
    android:layout_marginTop="107dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="150dp"
    android:layout_height="200dp"
    android:id="@+id/buttonPic"
    android:visibility="visible"
    android:background="@null"
    android:layout_below="@+id/tapcrownTextPic"
    android:layout_alignLeft="@+id/tapcrownTextPic"
    android:layout_alignStart="@+id/tapcrownTextPic"
    android:layout_marginTop="25dp"
    android:onClick="onPicButtonClicked" />

新信息

我注意到错误与按钮有关。你可以在截图中看到我已经重命名了它,但它仍然认为它是 "button"

只需添加 public void onScanButtonClicked(视图) { } SenderActivity2 class.

中的方法

您必须添加此方法,因为您在各自的 xml.

中定义了 onScanButtonClicked

我想通了。在将内容从 RecieverActivity 复制到 Sender activity 后,我忘记更改 onCreate bundle 中的布局,因此它获取了错误 Activity 的布局信息。应该是 setContentView(R.layout.activity_sender_2);不是 setContentView(R.layout.activity_recipient);