初學 Android 時最常見的編譯錯誤之一:
cannot make a static reference to the non-static method setAdapter(ListAdapter) from the type ListView
代碼寫成了 ListView.setAdapter(adapter) —— 用類名調用實例方法。setAdapter 是 ListView 對象的成員方法,必須通過實例化後的對象引用調用。
// 1. 先通過 findViewById 獲取實例
ListView listView = findViewById(R.id.my_list_view);
// 2. 再用對象調用方法
listView.setAdapter(adapter);
ListView.class、Math.max(),用類名調用。listView.setAdapter()、textView.setText(),必須 new 或 findViewById 獲得對象後調用。findViewById 拿到實例。android:id="@+id/list" 寫成 @id/list,前者定義新 ID,後者引用現有 ID。adapter 也要先實例化:ArrayAdapter<String> adapter = new ArrayAdapter<>(...)。記住:「先找到對象,再讓對象幹活」,這個習慣能消除 80% 的初學期 NullPointerException 與靜態調用錯誤。