2016-08-09 125 views
-1

我得到的錯誤爲null pointer exception,但根據我的檢查,循環流程正常。 我得到這個代碼錯誤java.lang.NullPointerException:嘗試在空對象引用上調用虛擬方法'int java.lang.Integer.intValue()'

bitmap = BitmapFactory.decodeResource(getResources(), MapId.getId(mapName)); 
mIndiaBitmapLoadedListener.onIndiaBimapLoaded(bitmap); 
map.setImageBitmapReset(bitmap, true); 

這是我MapId

public class MapId 
    { 
     public static HashMap<String,Integer> mapids = new HashMap<String, Integer>(); 
     public static void setHashMap() 
     {  
      mapids.put("Political Map",R.drawable.indiapoliticalbig); 
      mapids.put("Physical Map",R.drawable.physicalmap); 
      mapids.put("Railway Map", R.drawable.railwaymap); 
      mapids.put("Road Map", R.drawable.roadmap); 
      mapids.put("Tourist Map", R.drawable.touristmap); 
     } 
     public static int getId(String stateName) 
     { 
      return mapids.get(stateName); 
     } 
    } 

這是我Error log

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference 
    at com.compare.IndiaAtlas.MapId.getId(MapId.java:58) 
    at com.compare.IndiaAtlas.IndiaFragment.onCreateView(IndiaFragment.java:62) 
    at android.app.Fragment.performCreateView(Fragment.java:2053) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
    at android.app.BackStackRecord.run(BackStackRecord.java:834) 
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454) 
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5343) 
    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:905) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

我們加入sethashmap()Railway Map三個新的條目,Road Map & Tourist Map這些得到error但之前的條目物理和政治地圖正常運行。

+3

我喜歡你的錯誤狀態'Integer.intValue(事實)」:

import java.util.Collections; import java.util.HashMap; import java.util.Map; public class MapId { private static Map<String, Integer> mapids; static { mapids = new HashMap<String, Integer>(); mapids.put("Political Map", R.drawable.indiapoliticalbig); mapids.put("Physical Map", R.drawable.physicalmap); mapids.put("Railway Map", R.drawable.railwaymap); mapids.put("Road Map", R.drawable.roadmap); mapids.put("Tourist Map", R.drawable.touristmap); mapids = Collections.unmodifiableMap(mapids); } public static int getId(String stateName) { if (mapids.containsKey(stateName)) { return mapids.get(stateName); } return 0; } } 

,然後只用參考「,但是相關的代碼在帖子中沒有任何地方存在。投票以重複的方式結束,如果可能的話,由於缺乏背景。 –

回答

1

我想你應該初始化您的地圖是這樣的:一個空物體上 MapId.getId("Tourist Map");

+0

類型不匹配:無法從Map 轉換爲HashMap

+0

我編輯了代碼。 – fernandospr

+0

使用此代碼屬於類獲取錯誤。 –

相關問題