-4

I followed the steps from this and it crashes my app when running. Below is my entire code.

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        TextView tx = (TextView)findViewById(R.id.textView5);
        Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myriad_pro_regular.ttf");
        tx.setTypeface(tf);
        setContentView(R.layout.activity_main);
    }
}

When I add the below codes

TextView tx = (TextView)findViewById(R.id.textView5);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/myriad_pro_regular.ttf");
tx.setTypeface(tf);

The app crashes and I have no idea what I did wrong here.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33

2 Answers2

2

You have set content view after you initialized textView. This should be correct approach:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView tx = (TextView)findViewById(R.id.textView5);
Typeface tf = 
Typeface.createFromAsset(getAssets(),"fonts/myriad_pro_regular.ttf");
tx.setTypeface(tf);

}
Payal
  • 903
  • 1
  • 9
  • 28
0

At first put your text view initialization under setContentView.

This is working code

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        TextView tx = (TextView)findViewById(R.id.text);
        Typeface tf = Typeface.createFromAsset(getResources().getAssets(),"myriad_pro_regular.ttf");
        tx.setTypeface(tf);

    }
L.Petrosyan
  • 430
  • 4
  • 12