ajzaklija
ajzaklija

Reputation: 59

Trying to set custom font on TextView - nullpointer

I have no underlined code, but as soon as i start my app on emulator, i'm receiving these errors.

    FATAL EXCEPTION: main
Process: com.milos.sportisa, PID: 2735
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.milos.sportisa/com.milos.sportisa.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
at com.milos.sportisa.SplashActivity.onCreate(SplashActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
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:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

 

And this is my SplashActivity:

public class SplashActivity extends AppCompatActivity {
    private TextView etSportisa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
       /* ButterKnife.inject(this);
        mProgressBar.setIndeterminateDrawable(new foldingCirclesDrawable.Builder(this)
                .build());
        mProgressBar.setIndeterminateDrawable(new foldingCirclesDrawable.Builder(this)
                .colors(getResources().getIntArray(R.array.colors) //Array of 4 colors
                        .build()); */

        Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "fonts/esp_ital.ttf");
        etSportisa.setTypeface(myTypeFace);
        etSportisa = (TextView) findViewById(R.id.tv_sportisa);



        final ProgressDialog pd = new ProgressDialog(SplashActivity.this);
        pd.setMessage("Učitavanje...");
        pd.show();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                // Actions to do after 10 seconds
                pd.dismiss();

                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);
            }
        }, 2000);
    }
}

Special font is added in assets\font folder. I also added an image for the loading screen but i think that the font is cousin those issues

Upvotes: 1

Views: 5480

Answers (2)

Janak
Janak

Reputation: 615

Make the following changes to your code:

Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "fonts/esp_ital.ttf");
etSportisa = (TextView) findViewById(R.id.tv_sportisa);
etSportisa.setTypeface(myTypeFace);

After initialization, you can do any operation with any layout.

Upvotes: 8

Shank
Shank

Reputation: 1398

The problem is here:

etSportisa is null

set it after you findViewByID

Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "fonts/esp_ital.ttf");
        etSportisa = (TextView) findViewById(R.id.tv_sportisa);
        etSportisa.setTypeface(myTypeFace);

Upvotes: 1

Related Questions