protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_orbital);
Intent i = getIntent();
Integer[] orbitalBodies = {R.drawable.dres, R.drawable.duna, R.drawable.eeloo, R.drawable.eve, R.drawable.jool, R.drawable.kerbin, R.drawable.kerbol, R.drawable.moho};
int planetId = i.getExtras().getInt("intOrbitalId");
ImageView img = (ImageView)findViewById(R.id.image);
img.setImageResource(orbitalBodies[planetId]); //this throws nullpointerexception
}
在調試中,我能夠確認orbitalBodies [planetId]返回drawables的正確整數值。我不能讓過去的這個小問題setImageResource到一個可繪製數組拋出空指針異常
看起來'img'爲空。 – 2014-11-22 08:10:18
驗證在view_orbital佈局內存在R.id.image(ImageView)。 – MathanG 2014-11-22 08:50:44
麥克M釘了它。我將img設置爲佈局文件中不存在的對象。從來沒有我認爲它會編譯這個錯誤,所以我從來沒有想過要檢查它。 – 2014-11-23 03:24:51