Android ArrayList空对象引用
我得到这个错误: 04-30 10:32:26.665:E/AndroidRuntime(13605): java.lang.RuntimeException:无法启动活动 ComponentInfo{com.ribicinc.vzemalipusti/com.ribicinc.vzemalipusti.Skatle}: java.lang.NullPointerException:尝试调用接口方法 空对象引用上的“java.lang.Object java.util.List.get(int)” 我做错了什么 我的代码,第一个活动:Android ArrayList空对象引用,android,arraylist,nullreferenceexception,Android,Arraylist,Nullreferenceexception,我得到这个错误: 04-30 10:32:26.665:E/AndroidRuntime(13605): java.lang.RuntimeException:无法启动活动 ComponentInfo{com.ribicinc.vzemalipusti/com.ribicinc.vzemalipusti.Skatle}: java.lang.NullPointerException:尝试调用接口方法 空对象引用上的“java.lang.Object java.util.List.get(int)
List<Integer> skatlice = Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
...
if (cases[position] == 1) {
myIntent2 = new Intent(Game.this, Skatle.class);
vrednostskatel = 1;
myIntent2.putExtra("vrednostskatel", vrednostskatel);
skatlice.set(0, 1);
myIntent2.putIntegerArrayListExtra("skatlice", (ArrayList<Integer>) skatlice);
startActivity(myIntent2);
overridePendingTransition(R.layout.mainfadein, R.layout.splashfadeout);
}
List skatlice=Arrays.asList(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
...
如果(情况[位置]==1){
myIntent2=新意图(Game.this,Skatle.class);
VREDL=1;
myIntent2.putExtra(“vrednostskatel”,vrednostskatel);
skatlice.set(0,1);
myIntent2.PutIntegerArrayListXTRA(“skatlice”,(ArrayList)skatlice);
起始触觉(myIntent2);
覆盖渐变(R.layout.mainfadein,R.layout.splashfadeout);
}
第二项活动:
public class Skatle extends Activity {
int[] skatle = new int[28];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skatle);
Intent mIntent = getIntent();
int cases = mIntent.getIntExtra("vrednostskatel", 0);
List<Integer> skatlice = mIntent.getIntegerArrayListExtra("skatlice");
TextView text = (TextView) findViewById(R.id.editText1);
DecimalFormat formatter = new DecimalFormat("###,###,###");
for (int i = 0; i < 28; i++) {
int amount = skatlice.get(i);
skatle[i] = amount;
}
if (cases == 1 || skatle[0] == 1) {
TextView layout = (TextView) findViewById(R.id.textView1);
layout.setBackgroundResource(R.drawable.izbrano);
layout.setTextColor(Color.parseColor("#d7a308"));
ImageView image = (ImageView) findViewById(R.id.imageOdprto);
image.setImageResource(R.drawable.o1);
String strIi = formatter.format(cases);
text.setText(strIi + "€");
}
公共类Skatle扩展活动{
int[]skatle=新int[28];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skatle);
Intent minent=getIntent();
int cases=minent.getIntExtra(“vrednostscatel”,0);
List skatlice=minent.getIntegerArrayListExtra(“skatlice”);
TextView text=(TextView)findViewById(R.id.editText1);
DecimalFormat formatter=新的DecimalFormat(“####,###,##,##”);
对于(int i=0;i<28;i++){
int amount=skatlice.get(i);
skatle[i]=金额;
}
if(cases==1 | | skatle[0]==1){
TextView布局=(TextView)findViewById(R.id.textView1);
布局.立根资源(R.drawable.izbrano);
layout.setTextColor(Color.parseColor(#d7a308”);
ImageView image=(ImageView)findViewById(R.id.imageOdprto);
setImageResource(R.drawable.o1);
String strIi=formatter.format(大小写);
text.setText(strIi+“€”);
}
此崩溃发生在第二个活动中。您不能将基类强制转换为子类。该对象的第行没有ArrayList的信息:
myIntent2.putIntegerArrayListExtra("skatlice", (ArrayList<Integer>) skatlice);
在循环之前,请检查您的
skatlice
列表是否为null
。@Rami skatlice为null,不知道有什么问题。在第一个活动中尝试使用myIntent2.putExtra(“skatlice”,skatlice”);
,在第二个活动中尝试使用list skatlice=(list)minent.getSerializableExtra(“skatlice”);
。我获得方法putExtra类型Intent中的(字符串,布尔值)不适用于参数(字符串,列表)
atmyIntent2.putExtra(“skatlice”,skatlice);
仍然存在相同的问题。skatlice为null,我不知道如何修复该问题。
ArrayList<Integer> skatlice = new ArrayList<Integer>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) ;
myIntent2.putIntegerArrayListExtra("skatlice", skatlice);