I want to use custom fonts in my Android App.
Since I'm using these fonts in every TextView
, so I created my custom class DaSilvaAppTitleTextView
like this:
public class DaSilvaAppTitleTextView extends AppCompatTextView {
public DaSilvaAppTitleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/SixCaps.ttf");
this.setTypeface(tf);
}
public DaSilvaAppTitleTextView(Context context) {
super(context);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/SixCaps.ttf");
this.setTypeface(tf);
}
public DaSilvaAppTitleTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/SixCaps.ttf");
this.setTypeface(tf);
}}
I did the same for DaSilvaAppTitleTextView
with another font. Then I put them into my XML Layout files like that:
<mi.ur.de.dasilvaapp.DaSilvaAppTitleTextView
android:id="@+id/next_event_day"
style="@style/headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="@string/event_day_today" />
I always get the following exception:
08-31 10:34:30.281 2598-2598/mi.ur.de.dasilvaapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mi.ur.de.dasilvaapp, PID: 2598
java.lang.RuntimeException: Unable to start activity ComponentInfo{mi.ur.de.dasilvaapp/mi.ur.de.dasilvaapp.HomeActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to mi.ur.de.dasilvaapp.DaSilvaAppTitleTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to mi.ur.de.dasilvaapp.DaSilvaAppTitleTextView
at mi.ur.de.dasilvaapp.Fragments.Home_Fragment.initUI(Home_Fragment.java:72)
at mi.ur.de.dasilvaapp.Fragments.Home_Fragment.onStart(Home_Fragment.java:60)
at android.support.v4.app.Fragment.performStart(Fragment.java:1813)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:989)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1934)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:568)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1288)
at android.app.Activity.performStart(Activity.java:5974)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
The courious thing is that it works with my project partner who has the same code as I have. We collaborate over Git and we manually compared every single line of code and its exactly the same.
This is not possible. AppCompatTextView and your CustomTextView are siblings. Class cast in java is either upcast or downcast. Downcasting is only possible if at some point in your code you reference the child class. Hope this helps.
Android Developer has this to say about AppCompatTextView
:
This will automatically be used when you use
TextView
in your layouts. You should only need to manually use this class when writing custom views.
So you are doing it wrong. Just extend your CustomContentTextView
like:
public class CustomContentTextView extends AppCompatTextView
I already have tried overriding the used
TextView
methods, to solve theClassCastException
ClassCastException
is not thrown because you are not overriding some methods. The only reason you will get this exception is when the object can not be cast.
There were two cast exceptions and both were because of the same reason. Make your DaSilvaAppTitleTextView
extend AppCompatTextView
, like:
public class DaSilvaAppTitleTextView extends AppCompatTextView
Problem fixed!
I had unintentionally created a layout_v17 file, where normal TextView elements were used. I just deleted this file and then it worked.
So my approach was correct and my App has now CustomTextViews with my Custom Fonts.
But thanks for your help and advice, especially to @Sufian
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.