Your findViewById failed, either because you didn't set the content view or because you use an id that doesn't exist. Remember that finish() does not end execution of the function- to do that you need to return, so if you go into that branch of your if/else you'll never have sent a content view and findViewById will always return null.
Android NPE TypeFace
Вопрос
I set a font from the assets folder for each present the Activity button, I do not understand why I get a NPE at line 56 which is this:pswET.setTypeface(tf);
where am I wrong?
04-25 18:18:49.983: E/AndroidRuntime(1853): Caused by: java.lang.NullPointerException enter code here -25 18:18:49.983: E/AndroidRuntime(1853): at main.Login.onCreate(Login.java:56)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPrefs = getSharedPreferences("SharedPrefName", MODE_PRIVATE);
boolean value;
String name = getPackageName() + "_PREFERENCES";
SharedPreferences sp = getSharedPreferences(name, MODE_PRIVATE);
//control if there is no password
if (!sp.contains(Util.PREF_PSW) || "".equals(sp.getString(Util.PREF_PSW, null))) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
//check if the key is stored yes / no for the password
} if (value = sharedPrefs.getBoolean("check", true)) {
setContentView(R.layout.tastiera_password);
}else {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
Typeface tf=Typeface.createFromAsset(getAssets(),"roboto.ttf");
pswET = (TextView) findViewById(R.id.psw);
pswET.setTypeface(tf);
bb1 = (Button) findViewById(R.id.b1);
bb1.setTypeface(tf);
bb2 = (Button) findViewById(R.id.b2);
bb2.setTypeface(tf);
bb3 = (Button) findViewById(R.id.b3);
bb3.setTypeface(tf);
bb4 = (Button) findViewById(R.id.b4);
bb4.setTypeface(tf);
bb5 = (Button) findViewById(R.id.b5);
bb5.setTypeface(tf);
bb6 = (Button) findViewById(R.id.b6);
bb6.setTypeface(tf);
bb7 = (Button) findViewById(R.id.b7);
bb7.setTypeface(tf);
bb8 = (Button) findViewById(R.id.b8);
bb8.setTypeface(tf);
bb9 = (Button) findViewById(R.id.b9);
bb9.setTypeface(tf);
bb0 = (Button) findViewById(R.id.b0);
bb0.setTypeface(tf);
// Set the listener for all the buttons
int numberButtons[] = { R.id.b1, R.id.b2, R.id.b3,
R.id.b4, R.id.b5, R.id.b6, R.id.b7,
R.id.b8, R.id.b9, R.id.b0
};
NumberButtonClickListener numberClickListener = new NumberButtonClickListener();
for(int id : numberButtons) {
View v = findViewById(id);
v.setOnClickListener(numberClickListener);
}
}
private class NumberButtonClickListener implements OnClickListener {
@Override
public void onClick(View v) {
String text = (String) ((Button)v).getText();
setCurrentString(getCurrentString()+text);
}
public void setCurrentString(String s) {
currentString = s;
pswET.setText(s);
}
public String getCurrentString() {
return currentString;
}
}
Решение
Не связан с StackOverflow