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中文網其他相關文章!