NoSuchMethodException,尽管它在那里(NoSuchMethodException, despite it beeing there)
我正在开发一个可以使用外部apks的应用程序。 为此,我使用DexClassLoader将类从外部apks加载到
classes
-Array中,并使用如下所示的类:getFragment(){ for (Class<?> cls : classes) { Log.v("loadDexClasses", "Class loaded " + cls.getName()); if (cls.getName().contains("OpenQuestionFragment")) { Method m = null; Fragment xb = null; try { Class[] cArg = new Class[3]; cArg[0] = Integer.class; cArg[1] = String.class; cArg[2] = String[].class; m = cls.getMethod("getInstance",cArg); xb = (Fragment) m.invoke(null,INTNULL,STRINGNULL,STRINGARRAYNULL); showFragment(xb); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } if(xb==null){ return; } if (xb.equals(ClassLoader.getSystemClassLoader())) Log.v("loadDexClasses", "Same ClassLoader"); else Log.v("loadDexClasses", "Different ClassLoader"); } } } }
外部apk:
public class OpenQuestionFragment extends Fragment{ //flags final static int INTNULL = -1; final static String STRINGNULL = null; final static String[] STRINGARRAYNULL = null; static View view; static String setter; static boolean run = true; private static int edittextid; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RelativeLayout rl = new RelativeLayout(getActivity()); EditText et = new EditText(getActivity()); edittextid = view.generateViewId(); et.setId(edittextid); rl.addView(et); et.setTextSize(70); //blub et.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)et.getLayoutParams(); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE); et.setLayoutParams(layoutParams); et.setHint("hier eingeben"); view = rl; //inflater.inflate(R.layout.testlayout, container, false); if(setter!=STRINGNULL && setter != ""){ ((EditText) view.findViewById(edittextid)).setText(setter); } //initializeListenerThread(); return view; } public static OpenQuestionFragment newInstance(String seter){ /*setter=seter; run = true;*/ return new OpenQuestionFragment(); } public static Fragment getInstance(int intloader, String stringloader, String[] arrayloader){ setter = stringloader; return newInstance(null); } public String getResult(){ EditText ET = (EditText) view.findViewById(edittextid); return ET.getText().toString(); } public String getQuestionTag(){ return "OpenQuestion"; } public boolean isAnswered(){ EditText ET = (EditText) view.findViewById(edittextid); if(ET.getText().toString()!=""){ return false; }else{ return true; } } @Override public void onDestroy(){ run = false; super.onDestroy(); } }
和proguard,以防止未使用的方法组装:
-keepclassmembers class dexloader.openquestion.OpenQuestionFragment { public *; }
我已经知道,加载的类没有问题,因为“newInstance”函数可以被称为EXACT相同的方式,除了稍微调整cArg-Array,以匹配搜索后的方法,如下所示:
Class[] cArg = new Class[3]; cArg[0] = Integer.class; cArg[1] = String.class; cArg[2] = String[].class; m = cls.getMethod("getInstance",cArg); xb = (Fragment) m.invoke(null,INTNULL,STRINGNULL,STRINGARRAYNULL);
成
Class[] cArg = new Class[1]; cArg[0] = String.class; m = cls.getMethod("newInstance",cArg); xb = (Fragment) m.invoke(null,STRINGNULL);
当我启动应用程序时,它仍会抛出
NoSuchMethodException
,当我调用m.invoke();
被抓住了,好吧......不应该发生。 唯一的区别,我可以看到,getInstance()
被使用,但是proguard-rule
应该已经阻止了。感谢帮助
干杯,雅各布斯
I am working on an Application, which can work with external apks. For this i used the DexClassLoader , to load classes, from external apks into an
classes
-Array, and work with the classes like this:getFragment(){ for (Class<?> cls : classes) { Log.v("loadDexClasses", "Class loaded " + cls.getName()); if (cls.getName().contains("OpenQuestionFragment")) { Method m = null; Fragment xb = null; try { Class[] cArg = new Class[3]; cArg[0] = Integer.class; cArg[1] = String.class; cArg[2] = String[].class; m = cls.getMethod("getInstance",cArg); xb = (Fragment) m.invoke(null,INTNULL,STRINGNULL,STRINGARRAYNULL); showFragment(xb); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } if(xb==null){ return; } if (xb.equals(ClassLoader.getSystemClassLoader())) Log.v("loadDexClasses", "Same ClassLoader"); else Log.v("loadDexClasses", "Different ClassLoader"); } } } }
external apk:
public class OpenQuestionFragment extends Fragment{ //flags final static int INTNULL = -1; final static String STRINGNULL = null; final static String[] STRINGARRAYNULL = null; static View view; static String setter; static boolean run = true; private static int edittextid; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RelativeLayout rl = new RelativeLayout(getActivity()); EditText et = new EditText(getActivity()); edittextid = view.generateViewId(); et.setId(edittextid); rl.addView(et); et.setTextSize(70); //blub et.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)et.getLayoutParams(); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE); et.setLayoutParams(layoutParams); et.setHint("hier eingeben"); view = rl; //inflater.inflate(R.layout.testlayout, container, false); if(setter!=STRINGNULL && setter != ""){ ((EditText) view.findViewById(edittextid)).setText(setter); } //initializeListenerThread(); return view; } public static OpenQuestionFragment newInstance(String seter){ /*setter=seter; run = true;*/ return new OpenQuestionFragment(); } public static Fragment getInstance(int intloader, String stringloader, String[] arrayloader){ setter = stringloader; return newInstance(null); } public String getResult(){ EditText ET = (EditText) view.findViewById(edittextid); return ET.getText().toString(); } public String getQuestionTag(){ return "OpenQuestion"; } public boolean isAnswered(){ EditText ET = (EditText) view.findViewById(edittextid); if(ET.getText().toString()!=""){ return false; }else{ return true; } } @Override public void onDestroy(){ run = false; super.onDestroy(); } }
and the proguard, to prevent unused methods to not be assembled:
-keepclassmembers class dexloader.openquestion.OpenQuestionFragment { public *; }
i already know, that there isnt a problem with the loaded class, as the "newInstance" function can be called the EXACT same way, except tweaking the cArg-Array slightly, to match the sought after method, like follows:
Class[] cArg = new Class[3]; cArg[0] = Integer.class; cArg[1] = String.class; cArg[2] = String[].class; m = cls.getMethod("getInstance",cArg); xb = (Fragment) m.invoke(null,INTNULL,STRINGNULL,STRINGARRAYNULL);
into
Class[] cArg = new Class[1]; cArg[0] = String.class; m = cls.getMethod("newInstance",cArg); xb = (Fragment) m.invoke(null,STRINGNULL);
When i start the Application, it still throws a
NoSuchMethodException
, when i callm.invoke();
, which gets caught, and well... that isnt supposed to happen. The only difference, i can see is, thatgetInstance()
isnt used, but theproguard-rule
should have prevented that.Help is appreciated
Cheers, Jacobus
原文:https://stackoverflow.com/questions/34763275
最满意答案
使用RSpec 2.0.0.beta.19
# Gemfile group :test do gem "rspec", ">= 2.0.0.beta.19" gem "rspec-rails", ">= 2.0.0.beta.17" gem "shoulda" end # spec/spec_helper.rb require 'rspec/rails' require 'shoulda/integrations/rspec2' # Add this line # In your specs.... it { should validate_presence_of(:name) }
运行
rake spec
现在应该加载并运行包括RSpec 2匹配器在内的规范。Using RSpec 2.0.0.beta.19
# Gemfile group :test do gem "rspec", ">= 2.0.0.beta.19" gem "rspec-rails", ">= 2.0.0.beta.17" gem "shoulda" end # spec/spec_helper.rb require 'rspec/rails' require 'shoulda/integrations/rspec2' # Add this line # In your specs.... it { should validate_presence_of(:name) }
Running
rake spec
should now load and run specs including the RSpec 2 matchers.
相关问答
更多-
您的User模型没有声明belongs_to关联,因为您工厂认为它会: 你应该添加它: class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :r ...
-
assigns_to不适用于Ubuntu上的Rails 3 Shoulda(assigns_to doesn't work for Rails 3 Shoulda on Ubuntu)[2023-03-18]
您需要在声明之前调用subject { controller } 。 这实际上让我困惑了一段时间,我写了我的第一篇关于它的博客文章 。 You need to call subject { controller } before your it statements. This actually confused me so badly for a while that I wrote my first ever blog post about it. -
其实我发现了这个问题(但是我不太了解它),我没有使用正确的DLL。 我在C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0中使用DLL,而我应该使用C:\ Windows \ Microsoft.NET \ Framework \ v4.0.21006 \ WPF Actually I found the problem (however I don't really underst ...
-
rspec,autotest和Rails 3 beta 2找不到可执行的问题(rspec, autotest and Rails 3 beta 2 can't find executable issue)[2022-01-30]
这是rspec的一个问题。 Autospec需要一个二进制文件,以便它知道该怎么做。 在rspec 1.3.0中, 有一个 。 在rspec 2.0.0中,没有bin目录,所以没有autospec文件。 This blog post sorted me out: http://blog.davidchelimsky.net/2010/03/15/rspec-2-and-autotest/ autospec is deprecated in favour of autotest with some extra ... -
有趣的是,我只是在这里搜索flash问题,因为我在Rails 3 beta 3和RSpec 2中的身份验证黄瓜故事似乎在他们点击闪存通知的地方被打破了。 所以,我认为你是对的。 在使用RSpec2或RSpec2的Rails3中使用闪存通知时,有一些新技巧尚未实现。 如果您现在尝试将Cucumber故事移植到Rails3,那么问题很严重。 Funny, I was just searching here on the flash issue because my authentication cucumber ...
-
smack 4.2.0 Beta2与openfire 3.8.1兼容吗?(Is smack 4.2.0 Beta2 compatible with openfire 3.8.1?)[2021-12-14]
OpenFire 4.2使用以下代码响应聊天室创建 status code="110" status code="100' status code="201" OpenFire 3.8.1仅响应 status code="201" Smack 4.2.0.beta2期待状态码110,所以Smack 4.2.beta2无法用OpenFire 3.8.1创建聊天室,我发现问题是 Class: MultiUserChat Method: private Presence enter(MucEnterCo ... -
我一直在使用RVM ,它允许你运行多个Ruby,隔离一组宝石和其他配置。 对于测试和管理多个版本的Rails非常有用。 I have been using RVM which allows you to run multiple of Ruby with isolation for sets of gems and other configuration. Really useful for testing and managing multiple versions of Rails.
-
Heroku和Rails 3 beta(Heroku and Rails 3 beta)[2023-04-13]
Rails 3现在可以在heroku上使用 http://docs.heroku.com/rails3 干杯 Rails 3 is now available on heroku http://docs.heroku.com/rails3 cheers -
带有rspec2 beta 5和rails3 beta2的shoulda宏(shoulda macros with rspec2 beta 5 and rails3 beta2)[2023-11-18]
使用RSpec 2.0.0.beta.19 # Gemfile group :test do gem "rspec", ">= 2.0.0.beta.19" gem "rspec-rails", ">= 2.0.0.beta.17" gem "shoulda" end # spec/spec_helper.rb require 'rspec/rails' require 'shoulda/integrations/rspec2' # Add this line # In your specs ... -
在Visual Studio 2010 Beta2上安装asp.net mvc 2 beta(Install asp.net mvc 2 beta on Visual Studio 2010 Beta2)[2022-02-14]
在你提到的博客文章中,向下滚动,你会在评论中看到一个解决方法。 直接链接: http : //haacked.com/archive/2009/11/17/asp.net-mvc-2-beta-released.aspx#74907 In the blog post you mention, scroll down and you'll see a workaround posted in the comments. Direct link: http://haacked.com/archive/2009/ ...