2014-02-11 43 views
-1

所以交易是這樣的,我昨天發佈的應用程序商店,而我得到的空指針異常很多我ArrayAdapter奇怪的NullPointerException我不希望它:

適配器:

public class NewVenuesListRecyclingAdapterWithVenueObjects extends ArrayAdapter<Venue> 
{ 
.... 
public NewVenuesListRecyclingAdapterWithVenueObjects(Context context, int textViewResourceId, List<Venue> items) 
{ 
.... 
} 

public View getView(final int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder; 
    View currentView = convertView; 

    if(null == currentView) 
    { 
     LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     .... 
    } 
    else 
    { 
     holder = (ViewHolder)currentView.getTag(); 
    } 

    .... 
    if (UserLocation.instance.getLastKnownLocation() == null) { 
     holder.venueDistance.setText(""); 
     ((ViewGroup) currentView).requestLayout(); 
    } else { 
     // cups#202: `venueDistance` might be null in case we recycle a list item 
     // this is not really a fix, but should relatively okay (the distance will just not appear). 
     if (venuesList.get(position).getDistance() != 0.0f) { 
      holder.venueDistance.setVisibility(View.VISIBLE); 
      holder.venueDistanceUnit.setVisibility(View.VISIBLE); 
      if(FileAccessUtil.instance.getStringProperty(Consts.DISTANCE_UNIT).equals(Consts.DISTANCE_UNIT_KILOMETER)) 
      { 
       holder.venueDistance.setText(formatDistanceKilometer(activity, venuesList.get(position).getDistance())); 
       holder.venueDistanceUnit.setText(context.getResources().getString(R.string.distance_km)); 
       CupsLog.d(TAG, "chosen distance unit is km"); 

      } 
      else if(FileAccessUtil.instance.getStringProperty(Consts.DISTANCE_UNIT).equals(Consts.DISTANCE_UNIT_MILE)) 
      { 
       holder.venueDistance.setText(formatDistanceMiles(activity, venuesList.get(position).getDistance())); 
       holder.venueDistanceUnit.setText(context.getResources().getString(R.string.distance_mile)); 
       CupsLog.d(TAG, "chosen distance unit is mile"); 
      } 
     } 
    } 
    return currentView; 
} 

static class ViewHolder 
{ 
    TextView venueName; 
    TextView venueAddress; 
    TextView venueDistance; 
    TextView venueDistanceUnit; 
    ImageView venueImage; 
    ImageView venuebackImage; 
    ImageView venueMapIcon; 
    ImageView venueFavoriteIcon; 
    ProgressBar spinner; 
} 
} 

該行給我的例外是:

if(FileAccessUtil.instance.getStringProperty(Consts.DISTANCE_UNIT).equals(Consts.DISTANCE_UNIT_KILOMETER)) 

此行determins如果用戶希望以公里或英里顯示distnace根據他的選擇在設置頁面,FileAccessUtil是一個輔助類,其中包括有那些方法:

public void init(Context context) { 
    sharedPreferences = context.getSharedPreferences(Consts.PREFERENCSE_FILE_NAME, Activity.MODE_PRIVATE); 
} 

public String getStringProperty(String key) { 
    String res = null; 
    if (sharedPreferences != null) { 
     res = sharedPreferences.getString(key, null); 
    } 
    return res; 
} 

public void setStringProperty(String key, String value) { 
    if (sharedPreferences != null) { 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString(key, value); 
     editor.commit(); 
     CupsLog.i(TAG, "Set " + key + " property = " + value); 
    } 
} 

的帕塞爾的最終和平是我在Application類中設置該屬性:

public class App extends Application 
{ 
.... 

    public void onCreate() 
    { 
     super.onCreate(); 
     appObjInstance = this; 

     FileAccessUtil.instance.init(this); 
     if (FileAccessUtil.instance.getStringProperty(Consts.DISTANCE_UNIT) == null) 
     { 
      FileAccessUtil.instance.setStringProperty(Consts.DISTANCE_UNIT, Consts.DISTANCE_UNIT_KILOMETER); 
     } 
     .... 
    } 
} 

我可以ofcourse檢查空獲得期望之前,但我想,以避免會在ListView每個視圖運行適配器的內部此項檢查。

有人知道可能是什麼問題嗎?

+0

'FileAccessUtil.instance'是'null'?你永遠不會在你展示的代碼中分配它。爲什麼不調試並查看什麼是'null'? – m0skit0

+0

@ m0skit0,因爲我沒有遇到這個問題...我的用戶做...我無法調試一個問題,我沒有看到...我沒有得到這個異常,但我的很多用戶做。 –

+0

然後打開logcat,爲這些值添加一對日誌,並捕獲用戶的堆棧跟蹤。 – m0skit0

回答

2

如果你要這樣寫代碼:

if(FileAccessUtil.instance.getStringProperty(Consts.DISTANCE_UNIT).equals(Consts.DISTANCE_UNIT_KILOMETER)) 

然後,你需要確保getStringProperty(...)永遠不能返回null

public String getStringProperty(String key) { 
    String res = ""; 
    if (sharedPreferences != null) { 
     res = sharedPreferences.getString(key, null); 
    } 
    return res; 
} 

的問題是,當你想到的Consts.DISTANCE_UNIT將出現在用戶的設置中(當你說這個問題不會發生在你身上時,我相信你,可能是因爲它在您的設置中爲),但事實上並非如此。也許根本問題在於你的安裝,誰知道?但我懷疑你應該修改getStringProperty(...)返回一個空字符串,如果沒有值可以找到。

HTH

+0

** AND **'FileAccessUtil.instance'也永遠不爲空。 – m0skit0

+0

@ m0skit0,FileAccessUtil.instance不能爲null,因爲它的寫法是這樣的:public static final FileAccessUtil instance = new FileAccessUtil(); –

+0

我最好的猜測是,在某些時候,sharedPreferences變爲Null(通常應用程序沒有使用很多時間)並且getStringPropery返回null。我會檢查建議@J史蒂芬佩裏 –