空指針異常(一)


報空指針異常

10-17 09:45:41.209 13290-13290/com.supoin.testhard E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.supoin.testhard, PID: 13290
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.supoin.testhard/com.supoin.gpslibrary.GpsTestActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.supoin.gpslibrary.Application.getString(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:902)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.supoin.gpslibrary.Application.getString(int)' on a null object reference
        at com.supoin.gpslibrary.SectionsPagerAdapter.getPageTitle(SectionsPagerAdapter.java:77)
        at com.supoin.gpslibrary.GpsTestActivity.initActionBar(GpsTestActivity.java:969)
        at com.supoin.gpslibrary.GpsTestActivity.onCreate(GpsTestActivity.java:194)
        at android.app.Activity.performCreate(Activity.java:6042)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        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:902) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
10-17 09:45:41.219 13290-13290/com.supoin.testhard I/Process: Sending signal. PID: 13290 SIG: 9
  • 日志中指出報錯的位子為 return Application.get().getString(R.string.gps_status_tab);
 @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case GPS_STATUS_FRAGMENT:
                return Application.get().getString(R.string.gps_status_tab);
            case GPS_MAP_FRAGMENT://
                return Application.get().getString(R.string.gps_map_tab);
            case GPS_SKY_FRAGMENT:
                return Application.get().getString(R.string.gps_sky_tab);
        }
        return null; // This should never happen
    }

再跟蹤問題,是Application.get() 為null,那么就是Application 為空,進入Application.java文件中查看,發現是繼承的Application沒有注冊導致的,解決問題就是在Manifest.xml文件中進行注冊Application.

只需要給Application標簽增加個name屬性把自己的 Application的名字加入即可。

public class Application extends android.app.Application {//繼承的Application沒有注冊

    private static Application mApp;

    private SharedPreferences mPrefs;

    public static Application get() {
        return mApp;
    }
...

最后在引用的lib文件中的Manifest.xml文件中進行注冊Application.

<application
        android:name=".Application"
         ...
         ...

問題解決。

知識點##

Application 和Activity,Service一樣,是Android框架的一個系統組件,當Android程序啟動時系統會創建一個application對象,用來存儲系統的一些信息。通常我們是不需要指定一個Application的,這時系統會自動幫我們創建。如果需要創建自己的Application,也很簡單創建一個類繼承application並在manifest的application標簽中進行注冊(只需要給application標簽增加一個name屬性把自己的Application的名字定入即可)。

Android系統會為每個程序運行時創建一個Application類的對象且僅創建一個,可以說是單例模式的一個類,且application對象的生命周期是整個程序中最長的,他的生命周期就等於這個程序的生命周期。因為他是全局的單例,所以在不同的activity,service中獲得的對象都是同一個對象,所以通過application來進行一些,數據傳遞,數據共享,數據緩存等操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM