1

嗨,我正在尝试更改 a 的字体样式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

static Typeface myNewFace = Typeface.createFromAsset(getAssets(),"fonts/bediz__.ttf");

它给了我以下错误

Cannot make a static reference to the non-static method getAssets() from the type ContextWrapper

我不知道该怎么办,我以前做过几次这项工作,但现在我不知道为什么它不起作用。

4

2 回答 2

2

你必须这样做

static Typeface myNewFace = Typeface.createFromAsset(context.getAssets(),"fonts/bediz__.ttf"); 

其中 context 应该是调用适配器的类的上下文。

于 2012-06-19T16:00:03.537 回答
1

这是因为您已将您声明inner classstatic;使您inner class的顶级嵌套类不再是成员,因此,如果不首先通过对实例化对象的引用nested class;,您将无法再访问其中的任何non-static member内容。outer class

对于 a ,在创建for annon-static inner class时始终传递对外部对象的(隐藏)引用;因此可以访问外部的所有成员。对于 a ,此引用未通过。objectinner classobject/classstatic inner class

As to your sample, you could use the reference to the outer object that you are explicitly passing along when creating a new CustomListAdapter object: "adap = new CustomListAdapter(this);" but a better solution is probably to drop this static keyword from the inner class definition. You won't need to pass a reference to the outer object anymore either.

于 2012-06-19T17:04:17.427 回答