我想为TextView设置字体,TextView是GridView的适配器。我需要从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设置字体?或者在这方面指导我?
答案 0 :(得分:0)
好像你没有在setContentView
TextView listitem = (TextView) findViewById(R.id.listitem);
答案 1 :(得分:0)
R.id.listitem是网格项的布局?如果是这样,您需要在适配器的getView()方法中设置字体,而不是在onCreate中。在onCreate()期间,尚未创建网格项。这就是为什么findById返回null导致NPE。如果您需要更多帮助,请发布适配器的代码。
答案 2 :(得分: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);
}
}
答案 3 :(得分:0)
尝试使用以下代码来应用字体...但首先需要将适当的字体(您需要的)复制到资产/字体文件夹中。
TextView listitem = (TextView) findViewById(R.id.listitem);
font = Typeface.createFromAsset(this.getAssets(), "fonts/DroidSansFallback.ttf");
listitem.setTypeface(font);
listitem.setText("जहां आप जाना चाहते हैं");