Android 中的“尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'”
当 Activity 在完全初始化之前尝试访问视图时,会发生此错误。具体来说,当在 null 对象上调用 Window.getCallback() 方法时会触发该错误,如果尚未在 onCreate() 中调用 setContentView() 方法,则可能会发生这种情况。
原因:
为了防止此错误,声明视图字段而不在类中初始化它们很重要声明:
private EditText usernameField, passwordField; private TextView error; private ProgressBar progress;
然后,在调用 setContentView() 后,在 onCreate() 中为这些字段赋值:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameField = (EditText)findViewById(R.id.username); passwordField = (EditText)findViewById(R.id.password); error = (TextView)findViewById(R.id.error); progress = (ProgressBar)findViewById(R.id.progress); }
其他建议:
new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashActivity.this, LoginActivity.class); startActivity(intent); finish(); } }, 1500);
以上是为什么我在 Android 中收到'尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'”?的详细内容。更多信息请关注PHP中文网其他相关文章!