我从assets文件夹中为每个出现的Activity按钮设置了一个字体,我不明白为什么我在第56行得到一个NPE,这是:pswET.setTypeface(tf);
我哪里错了?
04-25 18:18:49.983:E / AndroidRuntime(1853):引起:java.lang.NullPointerException 在此处输入代码 -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;
}
}
答案 0 :(得分:0)
您的findViewById失败,原因是您没有设置内容视图,或者因为您使用的是不存在的ID。请记住,finish()不会结束函数的执行 - 需要返回,所以如果你进入if / else的那个分支,你将永远不会发送内容视图,findViewById将始终返回null