gpt4 book ai didi

java - 通过资源 ID 获取 String 时出现 Resource$NotFoundException

转载 作者:行者123 更新时间:2023-12-02 12:44:04 24 4
gpt4 key购买 nike

我遇到了这个错误

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 个子 Activity 制作一个适配器,每个 Activity 包含 4 个具有 3 到 6 个状态的对象(如上所示),其中之一是地址。这是我的 Activity 之一:

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);
}

最佳答案

首先,确保您的 getAddress 返回正确的“字符串资源 ID”。

为了从 res id 调用以下字符串获取字符串:

address.setText(getResources().getString("YOUR_INT_RES_ID"));

关于java - 通过资源 ID 获取 String 时出现 Resource$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44846396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com