2013-06-04 78 views
0

我有一個打包的庫.jar,我想從中訪問一個類。
但在param這需要Context,我是新的Android開發)。試圖通過(thisMainActivity,但結果是null。你能解釋爲什麼會發生這種情況以及如何解決?Android應用程序上下文參數

下面是示例代碼:

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

什麼是'this'有?活動?簡單的課程? –

+0

我們可以再看一點嗎?什麼是空?上下文還是ApiHttpClient? – AMerle

+0

是的,「MainActivity擴展Activity」,它不加載ApiHttpClient加載結果爲空,但沒有上下文。 – Daniel

回答

2

嗯,可能是你需要你的JAR添加到類加載器,然後作出的Class.forName像Java的做。

這裏例如

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(); 
    } 
+0

jar文件被添加爲eclipse庫,我可以查看它的所有類,但是當加載時,結果爲空。 – Daniel

+0

tryed Class.forName?什麼結果?它不能爲空。如果Jar加載爲庫。 –

+0

Class.forName(「com.vine.api.ApiHttpClient」) - > 06-04 10:46:13.582:W/dalvikvm(5935):異常Ljava/lang/NullPointerException;在初始化Lcom/vine/api/ApiHttpClient時拋出; – Daniel