0

I am getting this error on using a custom font. I have tried using the code in MainActivity and the Respective xml activity(Enquiry) too ( in this case there's no error but the font wont apply).

MainActivity

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    WebView webView;
   TextView tv_appname;
    Typeface typeface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String fontpath="font/batman.ttf";
        tv_appname=(TextView)findViewById(R.id.tv_appname);
        typeface= Typeface.createFromAsset(getAssets(),fontpath);
        tv_appname.setTypeface(typeface);

Acitivity Enquiry(In which the text is placed whose font I want to change)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    tools:context="mayank2511.galgotiasuniversity.EnquiryActivity"
    android:id="@+id/nav_enq"
    android:paddingLeft="20dp"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enquiry Form"
        android:textSize="34sp"
        android:textColor="#fff"
        android:id="@+id/tv_appname"
        android:layout_marginTop="10dp"
        android:gravity="center"/>

Enquiry Activity (incase it helps- it's commented rn)

public class EnquiryActivity extends AppCompatActivity {
   /* TextView tv_appname;
    Typeface typeface; */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enquiry);
        /*String fontpath="font/batman.ttf";
        tv_appname=(TextView)findViewById(R.id.tv_appname);
        typeface= Typeface.createFromAsset(getAssets(),fontpath);
        tv_appname.setTypeface(typeface);
        getSupportActionBar().hide();*/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

Font Directory and main activity

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Did you try to debug or check the stack trace to see which line exactly throws the NPE?

Other than tv_appname.setTypeface(typeface) (which will not throw a NPE if you're using the layout you provided), I see only one line that could throw a NPE and that is getSupportActionBar().hide(). You should always check if the action bar is not null first.

Gabriel Costa
  • 341
  • 1
  • 10
  • tv_appname.setTypeface(typeface) This is throwing the NPE that's the main problem. – Mayank Jain Jun 05 '17 at 14:46
  • This is because you declared tv_app... in the main Activity, rather than in the Activity to which it belongs. – Phantômaxx Jun 05 '17 at 14:48
  • Yeah. Apparently, you're using 2 different layouts (`activity_enquiry` and `activity_main`) but using the same code. Make sure you have a TextView with id `tv_appname` in both – Gabriel Costa Jun 05 '17 at 15:06