我有一個打包的庫.jar,我想從中訪問一個類。
但在param這需要Context
,我是新的Android開發)。試圖通過(this
)MainActivity
,但結果是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)
什麼是'this'有?活動?簡單的課程? –
我們可以再看一點嗎?什麼是空?上下文還是ApiHttpClient? – AMerle
是的,「MainActivity擴展Activity」,它不加載ApiHttpClient加載結果爲空,但沒有上下文。 – Daniel