Android- 无法从静态字段的资源中获取字符串 - 无法从静态上下文中引用非静态方法 getResources”
Android- cant get String from Resources for Static field- Non-static Method getResources cannot be referenced from a static-context"
我使用 launcherIcons class 在网格视图中传递图像和文本。作为下面的代码,工作正常。
但我想将字符串更改为
new LauncherIcon(R.mipmap.ic_launcher,getResources().getString(R.string.hello))
我想从 Resources(R.string.hello) 获取字符串,当执行 getResources 警告时 "Non-static Method getResources cannot be referenced from a static-context"
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怎么用?
提前致谢。
我要更改的是删除 LauncherIcon
class 中的 static
,它可能是这样的:
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"),
};
I passed images and text in Grid view with launcherIcons class. As the below codes, works fine. But I want to change the String as :
new LauncherIcon(R.mipmap.ic_launcher,getResources().getString(R.string.hello));
如果您在执行此操作时遇到错误,您可以称其为 doing(第一种方法,如果您将其更改为我的答案应该有效):
LauncherIcon(R.mipmap.ic_launcher, YOURCONTEXT.getResources().getString(R.string.hello));
我使用 launcherIcons class 在网格视图中传递图像和文本。作为下面的代码,工作正常。 但我想将字符串更改为
new LauncherIcon(R.mipmap.ic_launcher,getResources().getString(R.string.hello))
我想从 Resources(R.string.hello) 获取字符串,当执行 getResources 警告时 "Non-static Method getResources cannot be referenced from a static-context"
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怎么用? 提前致谢。
我要更改的是删除 LauncherIcon
class 中的 static
,它可能是这样的:
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"),
};
I passed images and text in Grid view with launcherIcons class. As the below codes, works fine. But I want to change the String as :
new LauncherIcon(R.mipmap.ic_launcher,getResources().getString(R.string.hello));
如果您在执行此操作时遇到错误,您可以称其为 doing(第一种方法,如果您将其更改为我的答案应该有效):
LauncherIcon(R.mipmap.ic_launcher, YOURCONTEXT.getResources().getString(R.string.hello));