ArrayList null object reference Android



我收到此错误:

04-30 10:32:26.665: E/Android运行时(13605): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.ribicinc.vzemialipusti/com.ribicinc.vzemialipusti.Skatle}: java.lang.NullPointerException:尝试调用接口方法 'java.lang.Object java.util.List.get(int)' on a null object reference

我做错了什么?

我的代码,第一个活动:

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);
}

第二项活动:

 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 + "€");
                }

此崩溃发生在第二个活动上。

不能只将基类转换为子类。该对象没有在线的 ArrayList 信息:

myIntent2.putIntegerArrayListExtra("skatlice", (ArrayList<Integer>) skatlice);

相反,请使用以下内容:

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);

相关内容

最新更新