IndexOutOfBounds ArrayList上的异常

时间:2018-01-28 06:24:28

标签: java android arraylist

我有一个带参数化构造函数和ArrayList作为参数的类。 ArrayList的大小在构造函数上有效,但onCreate方法的大小为零。怎么可能?

    public class ViewDetail extends AppCompatActivity {

    private static final String TAG = "val";
    final String POSITION_CLICKED="position";


    private ArrayList<DataModel> mData=new ArrayList<>();
    private Context mC;
    public ViewDetail( Context c,ArrayList<DataModel> data) {
        this.mData = data;
        this.mC = c;
        Log.v(TAG,"MDATA SIZE "+mData.size());
    }
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_detail);
    Log.v(TAG,"MDATA SIZE2 "+ mData.size());
        short pos = 0;
        try {
            pos = getIntent().getExtras().getShort(POSITION_CLICKED);
        } catch (Exception e) {
            e.printStackTrace();
        }
}}

1 个答案:

答案 0 :(得分:0)

  public ViewDetail( Context c,ArrayList<DataModel> data) {
        this.mData = data;
        this.mC = c;
        Log.v(TAG,"MDATA SIZE "+mData.size());
    }

错误!! ...不要创建扩展Activity的该类的构造函数。

  

您可以通过Intent

传递该数据,而不是上述内容