我有一个带有静态 final方法 getAll 的 Java 类:
public static final Vector<Category> getAll(Context context, ContentValues where) {
ArrayList<Integer> IDs = null;
if(where != null && where.containsKey(DatabaseAdapter.KEY_PRODUCT)) {
IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_PRODUCT_CATEGORY, new String[] { DatabaseAdapter.KEY_CATEGORY }, where);
} else {
IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_CATEGORIES, where);
}
Vector<Category> categories = new Vector<Category>();
for(Integer id: IDs) {
categories.add(Category.get(context, id));
}
return categories;
}
现在我想将 null 作为 where 语句的值,以便稍后在代码中忽略它。无论如何,在这个方法的测试用例中我有:
Vector<Category> categories = Category.getAll(context, null);
这又给了我一个NoSuchMethodError。我不知道它为什么这样做。我唯一能想象的是,我提交的 null 与上述方法的签名不匹配。但我怎样才能克服这个问题呢?我已经想到了重载。但这最终只会重写大部分代码。至少当我做的时候,我是怎么想的。
对此有什么建议吗?
菲尔
附注这是我得到的堆栈跟踪:
java.lang.NoSuchMethodError: com.sap.catalogue.model.Category.getAll
at com.sap.overture.test.model.CategoryTest.testGetAll(CategoryTest.java:59)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
最佳答案
如果该方法在编译时不存在,则代码将无法编译。
如果您在运行时收到 NoSuchMethodError
,则表明您正在运行的 Category
类的版本与 的版本不同您正在编译的 Category
类。
您的设置是什么样的 - 这个类是否在同一个项目中?您是否从另一个项目复制 JAR?
关于java - Java 如何为方法生成签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3839739/