I have created an XML layout with the name def_list
. I have assigned an id for it and that is def_list_textview
. I have put a font called bn.ttf
in the assets folder.
The XML layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/def_list_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right" >
</TextView>
But when I set a typeface for it using the code:
def_list_tv = (TextView)findViewById(R.id.def_list_textview);
Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/bn.ttf");
def_list_tv.setTypeface(tf);
I get a nullPointerException in the LogCat. I don't know why.
This is my LogCat:
12-26 17:14:56.496: E/AndroidRuntime(1254): FATAL EXCEPTION: main
12-26 17:14:56.496: E/AndroidRuntime(1254): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.adeveloper.handydic/net.adeveloper.dic.PortDef}: java.lang.NullPointerException
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.os.Looper.loop(Looper.java:123)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-26 17:14:56.496: E/AndroidRuntime(1254): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 17:14:56.496: E/AndroidRuntime(1254): at java.lang.reflect.Method.invoke(Method.java:507)
12-26 17:14:56.496: E/AndroidRuntime(1254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-26 17:14:56.496: E/AndroidRuntime(1254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-26 17:14:56.496: E/AndroidRuntime(1254): at dalvik.system.NativeStart.main(Native Method)
12-26 17:14:56.496: E/AndroidRuntime(1254): Caused by: java.lang.NullPointerException
12-26 17:14:56.496: E/AndroidRuntime(1254): at net.adeveloper.dic.PortDef.onCreate(PortDef.java:53)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-26 17:14:56.496: E/AndroidRuntime(1254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-26 17:14:56.496: E/AndroidRuntime(1254): ... 11 more
remove your typeface file from fonts/ and put it directly inside of Assets folder, should works! hope to help