gpt4 book ai didi

android - setImageResource 到可绘制对象数组会抛出空指针异常

转载 作者:行者123 更新时间:2023-11-29 00:15:37 26 4
gpt4 key购买 nike

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] 为可绘制对象返回了正确的整数值。我无法克服这个小问题

最佳答案

使用 int 数组代替 Integer 数组

int[] 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 = getIntent.getExtras().getInt("intOrbitalId");

if(planetId != null) {
ImageView img = (ImageView)findViewById(R.id.image);
img.setImageResource(orbitalBodies[planetId]);
}

因为

public static final class drawable 
{
public static final int ic_launcher=0x7f020000;
}

关于android - setImageResource 到可绘制对象数组会抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27075352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com