0

I am trying to add custom fonts in my android app.

LoginActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.app.Activity;
import android.widget.TextView;
import android.graphics.Typeface;

public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button login = (Button) findViewById(R.id.login);
        Button signup = (Button) findViewById(R.id.signup);
        TextView username = (TextView) findViewById(R.id.username);
        Typeface custom_font = Typeface.createFromAsset(this.getAssets(), "fonts/smartwatch.ttf");
        username.setTypeface(custom_font);
        login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent loginOnlyIntent = new Intent(view.getContext(), LoginOnlyActivity.class);
                startActivity(loginOnlyIntent);
            }
        });


        signup.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Intent signUpIntent = new Intent(view.getContext(), Signup.class);
                startActivity(signUpIntent);
            }
        });


    }
}

I am getting the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.LoginActivity}: java.lang.NullPointerException

I have added assests folder in the src/main folder too.In the assests folder is fonts folder in which I have font file smartwatch.tff

I have checked almost every answer available on SO but nothing worked so far for me.

update: After using Debugger, I found that

TextView username = (TextView) findViewById(R.id.username);

returns null and hence the NPE.

Adding layout file :

 <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="155.0dip"
            android:paddingTop="200.0dip"
            android:paddingRight="4.0dip"
            android:paddingBottom="1.0dip"
            android:layout_below="@+id/TextView02">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/UserName"
                android:id="@+id/username"/>
            <EditText
                android:id="@+id/edit_phone"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:inputType="number"
                android:hint="  "/>
 </LinearLayout>

Please help me why textview username is null

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
the_unknown_spirit
  • 2,518
  • 7
  • 34
  • 56

1 Answers1

2

Your code works fine for me. enter image description here


In LinearLayout you provided missing buttons, but I think you have added them in your project. In addition, I have error because you have omitted the XML namespace

xmlns:android="http://schemas.android.com/apk/res/android"

into Linear Layout.

Try to Invalidate and restart project, It 's strange the null pointer problem on TextView, You've done all right, unless the problem lies elsewhere.

Make sure you have created the Assets folder doing: Right click on "app" folder, new --> Folder --> Assets Folder

Community
  • 1
  • 1
Giupo
  • 62
  • 7