0

我想为 GridView 的适配器 TextView 设置字体。我需要从 Asset 文件夹访问字体。

TextView listitem = (TextView) findViewById(R.id.listitem);
listitem.setTypeface(Typeface.createFromAsset(this.getAssets(), "DroidSans4.ttf"));

我正面临这个错误列表。

> 
05-01 14:38:54.510: E/AndroidRuntime(9088): FATAL EXCEPTION: main
05-01 14:38:54.510: E/AndroidRuntime(9088): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andro.sqllite/com.andro.sqllite.UsingSQLiteActivity}: java.lang.NullPointerException
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.os.Looper.loop(Looper.java:123)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at java.lang.reflect.Method.invoke(Method.java:507)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at dalvik.system.NativeStart.main(Native Method)
05-01 14:38:54.510: E/AndroidRuntime(9088): Caused by: java.lang.NullPointerException
05-01 14:38:54.510: E/AndroidRuntime(9088):     at com.andro.sqllite.UsingSQLiteActivity.onCreate(UsingSQLiteActivity.java:92)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-01 14:38:54.510: E/AndroidRuntime(9088):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-01 14:38:54.510: E/AndroidRuntime(9088):     ... 11 more

在这行代码中,我面临这些错误:

listitem.setTypeface(Typeface.createFromAsset(this.getAssets(), "DroidSans4.ttf"));

谁能指导我如何为我的 GridView 设置字体?或者在这方面指导我?

4

4 回答 4

0

参考这个,

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/BPreplay.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}
于 2012-05-01T10:07:06.390 回答
0

尝试以下代码来应用字体...但首先您需要将适当的字体(您需要)复制到您的资产/字体文件夹中。

TextView listitem = (TextView) findViewById(R.id.listitem);
font = Typeface.createFromAsset(this.getAssets(), "fonts/DroidSansFallback.ttf");
listitem.setTypeface(font);
listitem.setText("जहां आप जाना चाहते हैं");
于 2012-05-01T10:10:17.407 回答
0

好像setContentView之前没打过电话TextView listitem = (TextView) findViewById(R.id.listitem);

于 2012-05-01T09:56:12.410 回答
0

R.id.listitem 是网格项的布局?如果是这样,您需要在适配器的 getView() 方法中设置字体,而不是在 onCreate 中。在 onCreate() 期间还没有创建网格项目。这就是 findById 返回 null 导致 NPE 的原因。如果您需要更多帮助,请发布适配器的代码。

于 2012-05-01T10:00:15.890 回答