所以交易是这样的,我昨天发布的应用程序商店,而我得到的空指针异常很多我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
每个视图运行适配器的内部此项检查。
有人知道可能是什么问题吗?
'FileAccessUtil.instance'是'null'?你永远不会在你展示的代码中分配它。为什么不调试并查看什么是'null'? – m0skit0
@ m0skit0,因为我没有遇到这个问题...我的用户做...我无法调试一个问题,我没有看到...我没有得到这个异常,但我的很多用户做。 –
然后打开logcat,为这些值添加一对日志,并捕获用户的堆栈跟踪。 – m0skit0