我有一个打包的库.jar,我想从中访问一个类
但在param这需要Context
,我是android dev中的新手。试图在this
上传递(MainActivity
),但结果为null
。你能解释为什么会发生这种情况以及如何解决?
以下是示例代码:
Context paramContext = this;
ApiHttpClient localApiHttpClient = (ApiHttpClient)paramContext.getSystemService("com.vine.api.ApiHttpClient");
if (localApiHttpClient == null)
localApiHttpClient = (ApiHttpClient)paramContext.getApplicationContext().getSystemService("com.vine.api.ApiHttpClient");
if (localApiHttpClient == null)
Log.d("loadState", "Result is null");
日志:
06-04 10:57:44.502: E/AndroidRuntime(6610): FATAL EXCEPTION: main 06-04 10:57:44.502: E/AndroidRuntime(6610): java.lang.ExceptionInInitializerError 06-04 10:57:44.502: E/AndroidRuntime(6610): at java.lang.Class.classForName(Native Method) 06-04 10:57:44.502: E/AndroidRuntime(6610): at java.lang.Class.forName(Class.java:217) 06-04 10:57:44.502: E/AndroidRuntime(6610): at java.lang.Class.forName(Class.java:172) 06-04 10:57:44.502: E/AndroidRuntime(6610): at com.example.dfgdf.MainActivity.onCreate(MainActivity.java:59) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.app.Activity.performCreate(Activity.java:5008) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.app.ActivityThread.access$600(ActivityThread.java:143) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.os.Handler.dispatchMessage(Handler.java:99) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.os.Looper.loop(Looper.java:137) 06-04 10:57:44.502: E/AndroidRuntime(6610): at android.app.ActivityThread.main(ActivityThread.java:4965) 06-04 10:57:44.502: E/AndroidRuntime(6610): at java.lang.reflect.Method.invokeNative(Native Method) 06-04 10:57:44.502: E/AndroidRuntime(6610): at java.lang.reflect.Method.invoke(Method.java:511) 06-04 10:57:44.502: E/AndroidRuntime(6610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 06-04 10:57:44.502: E/AndroidRuntime(6610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 06-04 10:57:44.502: E/AndroidRuntime(6610): at dalvik.system.NativeStart.main(Native Method) 06-04 10:57:44.502: E/AndroidRuntime(6610): Caused by: java.lang.NullPointerException 06-04 10:57:44.502: E/AndroidRuntime(6610): at com.vine.android.vineApplication.getHttpUserAgent(Unknown Source) 06-04 10:57:44.502: E/AndroidRuntime(6610): at com.vine.api.ApiHttpClient.(Unknown Source)
答案 0 :(得分:2)
嗯,您可能需要将JAR添加到ClassLoader,然后像Java一样创建Class.forName。
这里的例子
public static ClassLoader loadExternalJar(File file) throws Exception {
if (file.exists()) {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
ClassLoader retClassLoader;
retClassLoader = Thread.currentThread().getContextClassLoader();
method.invoke(retClassLoader, new Object[]{file.toURI().toURL()});
return retClassLoader;
} else {
throw new Exception("File not found with JAR:"+file.getAbsolutePath());
}
}
然后尝试调用Class.forName(" com.vine.api.ApiHttpClient")
试试这个:
try {
Class<?> someApiClass = Class.forName("com.vine.api.ApiHttpClient");
if (someApiClass != null) {
ApiHttpClient localApiHttpClient = (ApiHttpClient) someApiClass
.newInstance();
if (localApiHttpClient != null) {
// TODO Oeprations
Log.d("loadState", "Result is Good");
} else {
Log.d("loadState", "Result is null");
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}