如何从Android Oncreate方法访问这些变量

时间:2015-12-09 13:17:37

标签: java android arrays android-intent static

在我的代码中,我想从oncreate方法中获取这三个数组(stock,required,converted2)值。实际上我得到一个错误“试图获取空值的长度”。如果我给出的值为数组(blockquote)一切都很完美,但我希望这些数组从Intent获取它们的值。知道我哪里错了吗?

import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;

    import java.util.Arrays;
    import java.util.Arrays.*;
    import java.util.concurrent.Callable;



    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.math.*;
    import java.util.concurrent.Future;


    public  class activityvogel extends ActionBarActivity {

    private Button button;
        static int[] stock;
        static int[] required;
        static int[][] converted2;


 /* final  int[] required = {30, 20, 70, 30, 60};
             final  int[] stock = {50, 60, 50, 50};
             final  int[][] converted2 = {{16, 16, 13, 22, 17}, {14, 14, 13, 19, 15},
                     {19, 19, 20, 23, 50}, {50, 12, 50, 15, 11}};*/

        final   int nRows = stock.length;
        final   int nCols = required.length;

        boolean[] rowDone = new boolean[nRows];
        boolean[] colDone = new boolean[nCols];
        int[][] result = new int[nRows][nCols];
        static ExecutorService es = Executors.newFixedThreadPool(2);



        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_activityvogel);
                    Bundle extras = getIntent().getExtras();
             stock= extras.getIntArray("stock");
             required= extras.getIntArray("required");
            final String[][] cost = (String[][]) extras.getSerializable("converted");
            for (int index = 0; index < cost.length; index++)

            {
                converted2[index] = new int[cost[index].length];
                for (int subIndex = 0; subIndex < cost[index].length; subIndex++) {
                    converted2[index][subIndex] = Integer.parseInt(cost[index][subIndex]);
                                }
            }
            int supplyLeft = 0;
            for (int i = 0; i < stock.length; i++) {
                supplyLeft += stock[i];
            }
            int totalCost = 0;
            while (supplyLeft > 0) {
                int[] cell = new int[0];
                try {
                    cell = nextCell();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                int r = cell[0];
                int c = cell[1];
                int quantity = Math.min(required[c], stock[r]);
                required[c] -= quantity;
                if (required[c] == 0)
                    colDone[c] = true;
                stock[r] -= quantity;
                if (stock[r] == 0)
                    rowDone[r] = true;
                result[r][c] = quantity;


     supplyLeft -= quantity;
            totalCost += quantity * converted2[r][c];
        }

        System.out.println(Arrays.deepToString(result));

        System.out.println(totalCost);



    }

1 个答案:

答案 0 :(得分:0)

删除

final   int nRows = stock.length;
    final   int nCols = required.length;

int nRows , nCols ;

如果您只是想在onc​​reate()中设置它们......

stock 数组和必需数组未在声明中初始化。