2

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.

Bene
  • 21
  • 4
  • 1
    post `Home_Fragment.initUI` – Blackbelt Aug 28 '15 at 09:43
  • 3
    shouldn't `mi.ur.de.dasilvaapp.CustomContentTextView` be `mi.ur.de.dasilvaapp.DaSilvaAppContentTextView ` ? – Deh Aug 28 '15 at 09:48
  • @Deh This only a typing mistake – Bene Aug 28 '15 at 10:32
  • @Blackbelt there is only the initializing of the CustomTextView like this: CustomContentTextView example = (CustomContentTextView) getView().findViewById(R.id.example); – Bene Aug 28 '15 at 10:34
  • Can you show us the theme you set on this `HomeActivity`? Also, are you doing something like `return new AppCompatEditText(this, attrs);` as done in [this answer](http://stackoverflow.com/a/27455363/1276636)? – Sufian Aug 28 '15 at 11:52

3 Answers3

2

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.

anonymouse
  • 109
  • 2
  • 9
  • But why is this done this way in the following exmaples I found: http://stackoverflow.com/questions/9477336/how-to-make-custom-textview or http://www.101apps.co.za/index.php/articles/using-custom-fonts-in-your-android-apps.html – Bene Aug 28 '15 at 10:29
1

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

Edit

I already have tried overriding the used TextView methods, to solve the ClassCastException

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.

Edit 2

There were two cast exceptions and both were because of the same reason. Make your DaSilvaAppTitleTextView extend AppCompatTextView, like:

public class DaSilvaAppTitleTextView extends AppCompatTextView
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • Thanks for your suggestion, but this doesn`t solve the problem. The ClassCastExzeption remains in the same way. – Bene Aug 28 '15 at 12:18
  • Have you changed all your `extends TextView` to `extends CustomContentTextView`? Please also post the exception you are getting now. – Sufian Aug 28 '15 at 12:26
  • 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 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:65) at mi.ur.de.dasilvaapp.Fragments.Home_Fragment.onStart(Home_Fragment.java:54) – Bene Aug 28 '15 at 12:34
  • Add `DaSilvaAppTitleTextView` class to your original question. – Sufian Aug 28 '15 at 13:13
  • I don´t think that this will make a difference because this class is exactly the same. – Bene Aug 28 '15 at 15:17
  • It does. Did you notice that you were getting 2 cast exceptions before? Anyway, can you post the declaration of `DaSilvaAppTitleTextView`? Something like `public class CustomContentTextView extends TextView` will be enough. – Sufian Aug 28 '15 at 18:49
  • Please check my updated answer. Also read the quote from Android developer docs in my answer. – Sufian Aug 28 '15 at 20:01
  • Please update your code and exception in your original question. It is not an answer so please don't write it in my answer. :) – Sufian Aug 31 '15 at 07:30
0

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

Bene
  • 21
  • 4
  • I think this was another problem, the original being with the `extends`. What happens when you make `CustomContentTextView` and `DaSilvaAppTitleTextView` extend `TextView` instead of `AppCompatTextView` again? I believe it the original error will return. – Sufian Aug 31 '15 at 09:16