【问题标题】:ResourcesNotFoundException in Android appAndroid 应用程序中的 ResourcesNotFoundException
【发布时间】:2017-06-27 08:21:22
【问题描述】:

我有一个 Android 应用程序,由 Fragments 组成,一个作为主页,另外 4 个,每个片段都包含不同的地点和商店列表。所以我有 BarsFragment、CafesFragment、RestaurantsFragment 和 PlacesOf InterestFragment。

我为每个地点/商店创建了一个 Details 对象,其中包含图像、名称、地址以及(如果适用)电话号码和网址。

当用户单击列表项时,我希望我的应用程序打开与特定项相关的网页。我的问题是,对于我的应用程序的最后两个片段,我的应用程序崩溃并出现 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());

但是,我根本无法理解导致问题的原因......我完全确定我的 Fragments 中的代码没有问题。我什至将位置切换到我的片段,无论如何,当我滑动到最后两个片段中的任何一个时,应用程序都会崩溃(我重复一遍,无论我在那里放置哪些片段!)

你能帮我解决这个问题吗?

提前谢谢你。

编辑: 我想强调一下,我的问题发生在放置片段的特定位置。如果我将位于第二个位置的 BarsFragment(working) 与位于第四个位置的 RestaurantsFragment(crashing) 交换位置,则 BarsFragment 崩溃并且 RestaurantsFragment 正常工作。所以我相信这不是错误的资源 ID 或字符串等问题......

【问题讨论】:

  • 您的 String.xml 文件中是否有 ID 为“currentDetail.getWebAddress()”的资源?
  • 我调用我的 Details 构造函数并从我的 strings.xml 文件中获取所有数据。然后我使用“getWebAddress()”方法获取具体Details对象的网址。

标签: android android-fragments


【解决方案1】:

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

final String webAddress = currentDetail.getWebAddress();

【讨论】:

  • 您提出的代码行在我的情况下返回一个整数。 getWebAddress()方法返回相关字符串的资源id。
  • 如果是这种情况,请记录 currentDetail.getWebAddress() 并查看它返回的内容。
  • 正如我所说,它返回一个整数。这个整数可能是 webAddress 的资源 ID。
  • k.用 R.string.something 替换 currentDetail.getWebAddress()。如果它有效,那么错误可能在 getWebAddress() 中。
【解决方案2】:

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

的字符串

如果您想要将其转换为字符串,请使用

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

改为

【讨论】:

  • 不,这不正确。我已经尝试过你的建议,但我得到的是一个数字。我正在使用的代码似乎对于放置在第一个位置的所有片段都可以正常工作。
  • 但可能 currentDetail.getWebAddress() 不是 R.String 的有效 ID。
猜你喜欢
  • 2021-06-06
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 2015-03-06
  • 2014-03-15
  • 2011-08-24
相关资源
最近更新 更多