Android应用中的ResourcesNotFoundException

时间:2017-06-27 08:21:22

标签: android android-fragments

我有一个Android应用程序,包括Fragments,一个作为主页和另外4个,其中每个包含不同的地点和商店列表。所以我有BarsFragment,CafesFragment,RestaurantsFragment和PlacesOf InterestFragment。

对于每个地方/商店,我制作了一个详细信息对象,其中包含图像,名称,地址以及电话号码和网址(如果适用)。

当用户点击列表项时,我希望我的应用打开与特定项目相关的网页。我的问题是,对于我的应用程序的最后两个片段,我的应用程序崩溃了 ResourcesNotFoundException 并指向特定的代码行。让我分享一下我尝试实现逻辑的代码

/**
 * 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;
    }
}

问题似乎是以下代码行:

 final String webAddress = 
    this.getContext().getString(currentDetail.getWebAddress());

然而,我无法理解导致问题的原因......我完全相信我的片段中的代码是可以的。我甚至将位置切换到我的片段,无论如何,当我滑向最后两个中的任何一个时,应用程序崩溃(我再说一遍,无论我放在那里的碎片!)

你能帮帮我吗?

提前谢谢。

编辑: 我想强调我的问题发生在放置碎片的特定位置。如果我使用BarsFragment(工作),它处于第二个位置并切换到位于第四个位置的RestaurantsFragment(崩溃),那么BarsFragment崩溃并且RestaurantsFragment正常工作。所以我认为这不是错误的资源ID或字符串等问题......

2 个答案:

答案 0 :(得分:0)

您的资源(String.xml)中可能没有String,其id为 currentDetail.getWebAddress()

如果您要尝试将其转换为String,请使用

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

代替

答案 1 :(得分:0)

this.getContext().getString()方法接受资源ID作为参数,但您试图给它一个字符串。你应该给它一个资源ID。 只需使用

final String webAddress = currentDetail.getWebAddress();