我正嘗試從服務中啓動一項活動。在那個服務中,我做了幾件事(連接,評估等)。我需要啓動一個類的方法內的活動(「startActivity(IntentX)」),它總是返回我這個錯誤:由於上下文「爲空」,無法啓動活動?
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
我假設服務並不讓我得到的上下文。我在Android上搜索了很多關於Context的知識,但是我找不到解決方案。我試圖在MainActivity上聲明一個公共靜態上下文(就像我需要爲我的服務中的Location類所做的那樣),但即使在調試代碼時,也有變量上的數據,但每次都會返回該錯誤。
下面是完整的錯誤:再次
Process:... PID: 15991
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.danielsouto.crashdetector010.Servicio.DataCollector.run(DataCollector.java:36)
at com.example.danielsouto.crashdetector010.MainActivity.setup(MainActivity.java:86)
at com.example.danielsouto.crashdetector010.MainActivity$1.onClick(MainActivity.java:62)
at android.view.View.performClick(View.java:5210)
at android.view.View$PerformClick.run(View.java:21328)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
謝謝!
你能共享代碼?請 – therealprashant
請顯示代碼,這不是一個猜測業務。 –
請務必將'IntentX'聲明爲'Intent IntentX = new Intent(this,YourClass.class)' – sushildlh