I have an Android app, consisting of Fragments, one as the home page and 4 others with each one of them containing a different list of places and shops. So I have BarsFragment, CafesFragment, RestaurantsFragment and PlacesOf InterestFragment.
For each place/shop I made a Details object, containing an image, a name, an address and, if applicable, a phone number and a web address.
When the user clicks on a list item, I want my app to open the web page related to the specific item. My problem is that, for the last two Fragments of my app, my app crashes with a ResourcesNotFoundException and pointing to a specific line of code. Let me share the code where I try to implement my logic
/**
* Created by georgeampartzidis on 11/6/17.
* {@link} DetailsAdapter is an {@link} ArrayAdapter that provides the layout for each list
* based on a data source, which is a list of {@link} Details objects.
*/
public class DetailsAdapter extends ArrayAdapter<Details> {
public DetailsAdapter(Activity context, ArrayList<Details> details) {
//Here we initialize the ArrayAdapter's internal storage for the context and the list. We are
// doing so by calling the superclass constructor. The second argument is used when the Adapter
// generates a single TextView. Because we are writing our customized constructor, the
// constructor will not use the second argument, so it can be any value, for example 0.
super(context, 0, details);
}
@Override
public View getView(int position, final View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// get the data item associated with the specified position
final Details currentDetail = getItem(position);
//find the ImageView in the list_view.xml and set the ImageView to the image resource
//specified in the details
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
if (currentDetail.hasImage()) {
imageView.setImageResource(currentDetail.getResourceId());
imageView.setVisibility(View.VISIBLE);
}
//otherwise hide the imageView
else imageView.setVisibility(View.GONE);
//get the name od the specific details and set it on the nameTextView
TextView nameTextView = (TextView) listItemView.findViewById(R.id.name);
nameTextView.setText(currentDetail.getName());
//get the address of the specific details and set it om the addressTextView
TextView addressTextView = (TextView) listItemView.findViewById(R.id.address);
addressTextView.setText(currentDetail.getAddress());
//get the telephone number of the specific details and set it on the telNumberTextView
TextView telNumberTextView = (TextView) listItemView.findViewById(R.id.telephone);
final String webAddress = this.getContext().getString(currentDetail.getWebAddress());
Log.v("the web address is: ", webAddress);
if (currentDetail.hasPhone()) {
telNumberTextView.setText(String.valueOf(currentDetail.getTelNumber()));
telNumberTextView.setVisibility(View.VISIBLE);
} else telNumberTextView.setVisibility(View.GONE);
listItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (webAddress != null) {
Uri webPage = Uri.parse(webAddress);
Log.v("Web address:", webAddress);
Intent intent = new Intent(Intent.ACTION_VIEW, webPage);
v.getContext().startActivity(intent);
}
}
});
return listItemView;
}
}
The problem seems to be the following line of code:
final String webAddress =
this.getContext().getString(currentDetail.getWebAddress());
However, I can't understand what causes the problem at all... I am perfectly sure that the code in my Fragments is ok. I have even switched positions to my Fragments and, no matter what, the app crashes when I swipe to either of the last two (I repeat, no matter which Fragments I place there!)
Could you please help me with this?
Thank you in advance.
EDIT: I would like to stress that my problem occurs in specific positions where the Fragments are placed. If I take my BarsFragment(working), which is in the 2nd position and switch places with the RestaurantsFragment(crashing), which is in the 4th position, then the BarsFragment crashes and the RestaurantsFragment works properly. So i believe that it is not a matter of wrong resource Ids or strings etc...