我使用launcherIcons类在Grid视图中传递图像和文本。如下面的代码,工作正常。 但我想将String更改为
new LauncherIcon(R.mipmap.ic_launcher,getResources().getString(R.string.hello))
我想从Resources(R.string.hello)获取字符串,并在实现getResources时警告“无法从静态上下文引用非静态方法getResources”
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
String Activity="MainActivity";
Context activity_context=MainActivity.this;
static final LauncherIcon[] ICONS = {
new LauncherIcon(R.mipmap.ic_launcher,"Hello"),
new LauncherIcon(R.mipmap.ic_launcher, "About me"),
new LauncherIcon(R.mipmap.ic_launcher, "Venky"),
new LauncherIcon(R.mipmap.ic_launcher, "Noctilien"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//blah blah.............
}
LauncherIcon Class
static class LauncherIcon {
final String text;
final int imgId;
//final String map;
public LauncherIcon(int imgId, String text) {
super();
this.imgId = imgId;
this.text = text;
// this.map = map;
}
}
如何在那里使用getResources String? 在此先感谢。
答案 0 :(得分:1)
我要更改的内容是删除static
课程中的LauncherIcon
,它可能是这样的:
public class LauncherIcon {
final String text;
final int imgId;
public LauncherIcon(int imgId, String text) {
super();
this.imgId = imgId;
this.text = text;
}
}
然后您创建LauncherIcons
不带static
的数组,如下所示:
LauncherIcon[] ICONS = {
new LauncherIcon(R.mipmap.ic_launcher,"Hello"),
new LauncherIcon(R.mipmap.ic_launcher, "About me"),
new LauncherIcon(R.mipmap.ic_launcher, "Venky"),
new LauncherIcon(R.mipmap.ic_launcher, "Noctilien"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
new LauncherIcon(R.mipmap.ic_launcher, "Metro"),
new LauncherIcon(R.mipmap.ic_launcher, "RER"),
new LauncherIcon(R.mipmap.ic_launcher, "Bus"),
};
我使用launcherIcons类在Grid视图中传递图像和文本。如下面的代码,工作正常。但我想将String更改为:
new LauncherIcon(R.mipmap.ic_launcher,getResources()。getString(R.string.hello));
如果您在执行此操作时出错,您可以将其调用(如果您更改它,则第一种方式,因为我的答案应该有效):
LauncherIcon(R.mipmap.ic_launcher, YOURCONTEXT.getResources().getString(R.string.hello));