0

I am trying to add fonts to the screen and made a separate class for it.FontUtil class is where i am setting diff fonts.

This is the code from where I am instantiating the FontUtil class--

public class LoginActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_activity);

    FontUtil fontUtil = new FontUtil();
    fontUtil.loadFontsOnStartUp();
}

}

This is the Font Util Class--

public class FontUtil extends AppCompatActivity{

public void loadFontsOnStartUp(){

    TextView appName = (TextView) findViewById(R.id.app_name);
    Typeface customFontBerkshire = Typeface.createFromAsset(getAssets(), "fonts/berkshireswash-regular.ttf");
    appName.setTypeface(customFontBerkshire);

    Typeface customFontLatoLight = Typeface.createFromAsset(getAssets(), "fonts/Lato-Light.ttf");

    Button fbLogin = (Button) findViewById(R.id.facebook_login);
    fbLogin.setTypeface(customFontLatoLight);

    Button googleLogin = (Button) findViewById(R.id.google_login);
    googleLogin.setTypeface(customFontLatoLight);

    Button loginButton = (Button) findViewById(R.id.login_button);
    loginButton.setTypeface(customFontLatoLight);

    TextView forgotPasswordTextView = (TextView) findViewById(R.id.forgot_password);
    forgotPasswordTextView.setTypeface(customFontLatoLight);

    TextView signUpTextView = (TextView) findViewById(R.id.forgot_password);
    signUpTextView.setTypeface(customFontLatoLight);

    TextView termsConditonsTextView = (TextView) findViewById(R.id.forgot_password);
    termsConditonsTextView.setTypeface(customFontLatoLight);
}
}

And this is the error i am getting on the logs:

2:09:16.192 13678-13678/com.pixstory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.pixstory, PID: 13678 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pixstory/com.pixstory.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) at android.app.ActivityThread.access$900(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:152) at android.app.ActivityThread.main(ActivityThread.java:5497) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:118) at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:152) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:29) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:53) at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:31) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:184) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:518) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:189) at com.pixstroy.fonts.FontUtil.loadFontsOnStartUp(FontUtil.java:23) at com.pixstory.LoginActivity.onCreate(LoginActivity.java:24) at android.app.Activity.performCreate(Activity.java:6289) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)  at android.app.ActivityThread.access$900(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:152)  at android.app.ActivityThread.main(ActivityThread.java:5497)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

  • Do not make random subclasses of `Activity` (or, in this case, `AppCompatActivity`). And never create an instance of an `Activity` class yourself via the constructor, as the object will not be initialized properly. `FontUtil` should not be a subclass of `AppCompatActivity`. In fact, it should not even exist, as it is working with widgets that belong to `LoginActivity`. Move all of that code back into `LoginActivity` and eliminate `FontUtil`. – CommonsWare Aug 29 '17 at 16:45
  • Thanks for the help..:) – Bharat Sharma Sep 02 '17 at 21:20

0 Answers0