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

标签 java android

我遇到了这个错误

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/

相关文章:

android - 将 TURN 服务器添加到 android webRtc native

android - 更改小部件中的图像

java - Spring @OneToMany 问题

java - 方法返回可运行对象

Android:在 Activity 上下文之外使用 WebView

android - 带有包装文本的 TextView 的 getMeasuredHeight()

java - 如何在android中获取图像裁剪器的坐标?

java - 在 Windows cmd 中转义 jvm 参数

java - 为什么法语字符不能在 Java 中使用 utf-8?

java - Wildfly 中 2 个不同模块的 REST 路径相同