-1

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...

  • 2
    Possible duplicate of [Resources NotFoundException?](https://stackoverflow.com/questions/9468482/resources-notfoundexception) – MRX Jun 27 '17 at 08:22
  • Do you have a resource with the id of "currentDetail.getWebAddress()" in your String.xml file? – fbwnd Jun 27 '17 at 08:24
  • I call my Details constructor and fetch all the data from my strings.xml file. Then I use the "getWebAddress( )" method to get the web address of the specific Details object. – George Ampartzidis Jun 27 '17 at 08:42

2 Answers2

0

You probably don't have String in your resource (String.xml) with the id of currentDetail.getWebAddress()

If what you are trying to do is to convert it to String, use

final String webAddress = String.valueOf(currentDetail.getWebAddress());

instead

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
fbwnd
  • 687
  • 6
  • 12
  • No, this isn't correct. I have tried your suggestion, but what I get is a number. The code that I am using seems to work perfectly fine for all the fragments that are placed in the first positions. – George Ampartzidis Jun 27 '17 at 08:45
  • But probably the currentDetail.getWebAddress() is not a valid ID for R.String. – fbwnd Jun 27 '17 at 09:14
0

this.getContext().getString() method accepts resource id as parameter but you are trying to give it a string. you should give it a resource id. Just use

final String webAddress = currentDetail.getWebAddress();
henok
  • 864
  • 5
  • 12
  • The line of code that you are proposing returns an integer in my case. the getWebAddress( ) method returns the resource id of the string related. – George Ampartzidis Jun 27 '17 at 08:47
  • if that is the case log currentDetail.getWebAddress() and see what it is returning. – henok Jun 27 '17 at 08:57
  • As I said, it is returning an integer. This integer probably is the Resource Id of the webAddress. – George Ampartzidis Jun 27 '17 at 09:12
  • k. replace the `currentDetail.getWebAddress()` with R.string.something. if it works then the error is probably in getWebAddress(). – henok Jun 27 '17 at 09:21