【发布时间】: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对象的网址。