我遇到了这个错误
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:244)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3940)
at com.example.android.tourrior.TourAdapter.getView(TourAdapter.java:43)
错误说我的问题就在这里
TextView address = (TextView) listItemView.findViewById(R.id.address);
if (currentPosition.hasAddress()) {
address.setText(currentPosition.getAddress());
} else {
address.setVisibility(address.GONE);
}
此处声明了getAddress方法
public class Tour {
private int mName;
private int mPhoneNumber;
private int mAddress;
private int mOpeningHours;
private int mDescription;
private int mImageResourceId;
private static final int UNAVAILABLE = -1;
public Tour(int name,int address, int description, int phoneNumber, int openingHours, int image) {
mName = name;
mAddress = address;
mDescription = description;
mPhoneNumber = phoneNumber;
mOpeningHours = openingHours;
mImageResourceId = image;
}
public Tour(int name,int description, int openingHours, int image) {
mName = name;
mDescription = description;
mOpeningHours = openingHours;
mImageResourceId = image;
}
public Tour(int name, int address, int openingHours){
mName = name;
mAddress = address;
mOpeningHours = openingHours;
}
public Tour(int name,int address, int description, int openingHours, int phoneNumber) {
mName = name;
mAddress = address;
mDescription = description;
mOpeningHours = openingHours;
mPhoneNumber = phoneNumber;
}
public int getName() {
return mName;
}
public int getAddress() {
return mAddress;
}
public int getDescription() {
return mDescription;
}
public int getOpeningHours() {
return mOpeningHours;
}
public int getPhoneNumber() {
return mPhoneNumber;
}
public int getImageResourceId() {
return mImageResourceId;
}
我尝试将错误行中的代码更改为此
address.setText("" + currentPosition.getAddress());
它返回0.我正在为4个子活动创建一个适配器,每个包含4个具有3到6个状态的对象(如上所示),其中一个是地址。这是我的一项活动:
public class MallsActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places);
ArrayList<Tour> tour = new ArrayList<Tour>();
tour.add(new Tour(R.string.mall1_name,R.string.mall1_address,R.string.mall1_opening_hours));
tour.add(new Tour(R.string.mall2_name,R.string.mall2_address,R.string.mall2_opening_hours));
tour.add(new Tour(R.string.mall3_name,R.string.mall3_address,R.string.mall3_opening_hours));
tour.add(new Tour(R.string.mall4_name,R.string.mall4_address,R.string.mall4_opening_hours));
TourAdapter adapter = new TourAdapter(this, tour);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
} 我的适配器直到地址视图
public class TourAdapter extends ArrayAdapter<Tour> {
public TourAdapter(Context context, ArrayList<Tour> tour) {
super(context, 0, tour);
}
@Override
public View getView(int position, 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);
}
Tour currentPosition = getItem(position);
TextView name = (TextView) listItemView.findViewById(R.id.name);
name.setText(currentPosition.getName());
TextView description = (TextView) listItemView.findViewById(R.id.description);
if (currentPosition.hasDescription()) {
description.setText(currentPosition.getDescription());
} else {
description.setVisibility(View.GONE);
}
TextView address = (TextView) listItemView.findViewById(R.id.address);
if (currentPosition.hasAddress()) {
address.setText(currentPosition.getAddress());
} else {
address.setVisibility(View.GONE);
}
答案 0 :(得分:1)
首先,确保你的getAddress返回正确的“String resource Id”。
要从res id调用获取String,请执行以下操作:
address.setText(getResources().getString("YOUR_INT_RES_ID"));
答案 1 :(得分:0)
尝试简化您的课程。
public class Tour {
private int mName;
private int mPhoneNumber;
private int mAddress;
private int mOpeningHours;
private int mDescription;
private int mImageResourceId;
private static final int UNAVAILABLE = -1;
public Tour(int name,int address, int description, int phoneNumber, int openingHours, int image) {
mName = name;
mAddress = address;
mDescription = description;
mPhoneNumber = phoneNumber;
mOpeningHours = openingHours;
mImageResourceId = image;
}
public Tour(int name,int description, int openingHours, int image) {
this(name, 0, description, 0, openingHours, image);
}
public Tour(int name, int address, int openingHours){
this(name, address, 0, 0, openingHours, 0);
}
public Tour(int name,int address, int description, int openingHours, int phoneNumber) {
this(name, address, description, phoneNumber, openingHours, 0);
}
// Getters, setters
}
然后,您应该在调试器中设置一个断点
Tour currentPosition = getItem(position);
检查该对象的每个字段。该错误指出其中一个返回0或不存在的ID值。
注意:您可以简化该对象添加...
final Resources res = getResources();
final String packageName = getPackageName();
for (int i = 1; i <= 4; i++) {
String resName = "mall"+i;
tour.add(
new Tour(
res.getIdentifier(resName+"_name", "string", packageName),
res.getIdentifier(resName+"_address", "string", packageName),
res.getIdentifier(resName+"_opening_hours", "string", packageName)));
}
答案 2 :(得分:0)
答案在于我认为不重要且没有发布的地方,这里是
public boolean hasImage(){
return mImageResourceId != UNAVAILABLE;
}
public boolean hasDescription(){
return mDescription != UNAVAILABLE;
}
public boolean hasPhoneNumber(){
return mPhoneNumber != UNAVAILABLE;
}
public boolean hasAddress(){
return mAddress != UNAVAILABLE;
}
} 这是hasDescription方法的示例
TextView description = (TextView) listItemView.findViewById(R.id.description);
if (currentPosition.hasDescription()) {
description.setText(currentPosition.getDescription());
} else {
description.setVisibility(View.GONE);
}
我在本节中尝试做的是检查我的对象是否包含这些字段,因为它们彼此不同。我将UNAVAILABLE常量设置为-1并期望如果ID指向无处,它将返回-1并擦除该字段。但是,实际上它返回0,使得我的方法无用,因为它永远不会返回-1,无论如何都会存在该字段。所以我修复的只是更改UNAVAILABLE常量
private static final int UNAVAILABLE = 0;
这就是全部