我使用DexClassLoader類使用DexClassLoader
final String libPath = Environment.getExternalStorageDirectory() + "/test.jar";
final File tmpDir = getDir("dex", 0);
final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("org.shlublu.android.sandbox.MyClass");
final Object myInstance = classToLoad.newInstance();
final Method doSomething = classToLoad.getMethod("doSomething");
doSomething.invoke(myInstance);
在我的jar文件我正在印刷這是工作fine.Now我想做一些日誌從SD卡運行時加載一個jar文件動態加載庫交互要做的是從jar文件中打印Toast。雖然這樣做,我得到的異常是java.lang.reflect.InvocationTargetException。我知道爲什麼我們得到這個異常,並且它背後的原因是在打印吐司時使用它的上下文中的空指針異常。因此,它不是
What could cause java.lang.reflect.InvocationTargetException?
其背後原因的重複是
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
代碼的jar文件
public class MyClass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public void doSomething() {
Toast.makeText(getApplicationContext(), "MyClass: doSomething() called.", Toast.LENGTH_LONG).show();
Log.e(MyClass.class.getName(), "MyClass: doSomething() called.");
}}
誰能幫助我實現this.Any幫助會讚賞。
編輯: 我想要做的是我有我自己的圖書館,我的許多客戶正在使用...我想要的是從用戶的SD卡加載我的圖書館..我想在沒有用戶的知識且沒有任何版本更新的情況下更新圖書館。圖書館包括幾個界面,碎片和活動。所以現在我可以從SD卡加載我的庫並且可以調用基本函數。現在主要的挑戰是從庫中實現接口並調用在其中使用上下文的函數。
任何涉及此類操作的示例或提示都將非常有幫助。
的可能重複[這是什麼原因Java的。 lang.reflect.InvocationTargetException?](http://stackoverflow.com/questions/6020719/what-could-cause-java-lang-reflect-invocationtargetexception) – Emil
@Boss異常不是我的問題here.i知道它的起因。我需要幫助來實現所需的東西..所以它不是重複的那個java.lang.reflect.InvocationTargetException問題 –
...是的,它是一個重複的...你需要解開這個異常,以瞭解是什麼導致它... – Selvin