Java getResources()正在引发错误
我有以下代码,用于在我的应用程序中创建带有两个选项卡的自定义Java getResources()正在引发错误,java,android,Java,Android,我有以下代码,用于在我的应用程序中创建带有两个选项卡的自定义列表视图: 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; }
列表视图
:
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));
您需要更改代码,以便有对当前上下文的引用,这反过来将使您能够访问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));
当你这样做的时候
getResources()
这意味着
this.getResources()
这里这是SetRows(您的类)的一个实例,
并且您的类SetRows没有名为getResources的方法
这就是错误的含义。当您这样做时
getResources()
这意味着
this.getResources()
这里这是SetRows(您的类)的一个实例,
并且您的类SetRows没有名为getResources的方法
这就是错误的含义。getResources()
是一种可用于扩展上下文的所有类的方法。你们班没有
常见的解决方法是在创建SetRows
对象时传递上下文的实例
public SetRows(Context context, int image, String name, String id) {
super();
this.image = image;
this.name = Html.fromHtml(context.getResources().getString(R.string.colorcol)) + " COLOR: \n\t" + name;
this.id = "MEANS: \n\t" + id;
}
然后在您的活动中,您只需执行以下操作:
new SetRows(this, /****/);
getResources()
是一种可用于扩展上下文的所有类的方法。你们班没有
常见的解决方法是在创建SetRows
对象时传递上下文的实例
public SetRows(Context context, int image, String name, String id) {
super();
this.image = image;
this.name = Html.fromHtml(context.getResources().getString(R.string.colorcol)) + " COLOR: \n\t" + name;
this.id = "MEANS: \n\t" + id;
}
然后在您的活动中,您只需执行以下操作:
new SetRows(this, /****/);
通过构造函数传入上下文
,并调用:
context.getResources(...);
通过构造函数传入上下文
,并调用:
context.getResources(...);
您必须将其更改为contents.add(新的SetRows(this,inIconShow,sColor,sexplane));您必须将其更改为contents.add(新的SetRows(this,inIconShow,sColor,sexplane));我到处玩,把它修好了。ThanksI四处游荡并修复了它。谢谢ZouZou和/或DigCamara的回复,可能更有用。请参阅ZouZou和/或DigCamara的回复,可能更有用。