2014-02-11 38 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史蒂芬佩里 –