2015-02-23 65 views
-2

我開發在Android上的應用程序,我試圖運行通過仿真器,但每次它告訴我,它已停止工作時間的應用程序,我得到這些線路:的Android工作室應用開發

02-22 23:22:21.977 2170-2170/com.example.sarvagyagupta1.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.sarvagyagupta1.myapp, PID: 2170 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sarvagyagupta1.myapp/com.example.sarvagyagupta1.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: 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:105) 
      at com.example.sarvagyagupta1.myapp.MainActivity.<init>(MainActivity.java:61) 
      at java.lang.reflect.Constructor.newInstance(Native Method) 
      at java.lang.Class.newInstance(Class.java:1572) 
      at android.app.Instrumentation.newActivity(Instrumentation.java:1065) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
device not found 

請有人幫我解決這個問題。

+0

在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105) – 2015-02-23 04:30:21

+0

getApplicationContext()」上一個空對象引用 哪裏可以找到源代碼?它是.java代碼還是哪一個? – 2015-02-23 04:30:48

+0

把你的源代碼也 – 2015-02-23 04:59:26

回答

0

在調用onCreate之前,您無法調用getApplicationContext()。在此之前,該活動尚未完全初始化。

+0

那麼我該如何糾正? – 2015-02-23 04:59:46

+0

不會初始化您調用它的任何內容,直到onCreate。 – 2015-02-23 05:00:37

+0

我在哪裏可以找到這個getApplicationContext()? – 2015-02-23 05:12:42