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