您好我正在尝试更改TextView
的字体样式。我知道如何改变它,我在使用下面的代码之前已经完成了这个。
public class Main_Activity extends ListActivity {
Typeface myNewFace = Typeface.createFromAsset(getAssets(),
"fonts/bediz__.ttf");
private CustomListAdapter adap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adap = new CustomListAdapter(this);
setListAdapter(adap);
}
public static class CustomListAdapter extends BaseAdapter implements
Filterable {
public View getView(final int position, View convertView,
ViewGroup parent) {
textView.setText(prayers[position]);
holder.textLine.setTypeface(myNewFace);
}
}
我跳过的部分代码是因为没有必要,顺便提一下myNewFace
访问getView()
时,它要求我static
,当我创建static
时1}}喜欢这个
static Typeface myNewFace = Typeface.createFromAsset(getAssets(),"fonts/bediz__.ttf");
它给我以下错误
Cannot make a static reference to the non-static method getAssets() from the type ContextWrapper
我不知道该怎么做,我以前做了好几次这个工作,但现在我不知道为什么它不起作用。
答案 0 :(得分:2)
你必须这样做
static Typeface myNewFace = Typeface.createFromAsset(context.getAssets(),"fonts/bediz__.ttf");
其中context应该是类的上下文,它可以调用适配器。
答案 1 :(得分:1)
这是因为您已将inner class
声明为static;
,使您的inner class
成为顶级嵌套类,而不再是成员nested class;
因此,您无法访问任何non-static member
的任何outer class
,如果没有首先通过对实例化对象的引用,则会更长。
对于non-static inner class
,当创建object
的{{1}}时,始终会传递对外部对象的(隐藏)引用;因此,可以访问外部inner class
的所有成员。对于object/class
,此引用不会传递。
对于您的示例,您可以在创建新的CustomListAdapter对象时使用对显式传递的外部对象的引用:“static inner class
”但更好的解决方案可能是从此处删除此静态关键字内部类定义。您不再需要传递对外部对象的引用。