gpt4 book ai didi

java - getResources() 抛出错误

转载 作者:行者123 更新时间:2023-12-01 18:40:00 28 4
gpt4 key购买 nike

我有以下代码,用于在我的应用程序中创建带有两个选项卡的自定义 ListView:

package com.test.testing;

import android.content.Context;
import android.text.Html;

public class SetRows {
int image;
String name;
String id;

public int getImage () {
return image;
}

public void setImage (int image) {
this.image = image;
}

public String getName () {
return name;
}

public void setName (String name) {
this.name = name;
}

public String getID () {
return id;
}

public void setID (String id) {
this.id = id;
}

public SetRows(int image, String name, String id) {

super();
this.image = image;
this.name = Html.fromHtml(getResources().getString(R.string.colorcol)) + " COLOR: \n\t" + name;
this.id = "MEANS: \n\t" + id;
}

}

以下行:

this.name = Html.fromHtml(getResources().getString(R.string.colorcol)) + "COLOR:\n\t"+ name;

给我以下错误:

SetRows 类型的 getResources() 方法未定义

最佳答案

您需要更改代码,以便获得对当前上下文的引用,从而使您能够访问 getResources

例如

public SetRows(Context currentContext,int image, String name, String id) {

super();

this.image = image;
this.name = Html.fromHtml(currentContext.getResources().getString(R.string.colorcol)) + " COLOR: \n\t" + name;
this.id = "MEANS: \n\t" + id;
}

您必须将类的实例化更改为

contents.add(new SetRows(this,inIconShow, sColor, sExplain)); 

关于java - getResources() 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20336432/

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