askerov / DynamicGrid

Drag and drop GridView for Android
Apache License 2.0
926 stars 288 forks source link

crash on drag in edit mode #46

Open ghost opened 9 years ago

ghost commented 9 years ago

I am dragging item to bottom of GridView and it crashes

fhur commented 9 years ago

Please add more details to help debug the issue: Include stack traces, code and steps to reproduce it.

ghost commented 9 years ago

Dragging in edit mode works well in sample. But its crashes when i modify the sample. modification in sample 1. Pass Integer ArrayList to Adapter and try to set image of drawable from ArrayList . Here is stack trace error .

11-05 02:36:37.310: E/AndroidRuntime(1132): java.lang.NullPointerException 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.widget.AdapterView.getPositionForView(AdapterView.java:597) 11-05 02:36:37.310: E/AndroidRuntime(1132): at org.askerov.dynamicgrid.DynamicGridView.getColumnAndRowForView(DynamicGridView.java:754) 11-05 02:36:37.310: E/AndroidRuntime(1132): at org.askerov.dynamicgrid.DynamicGridView.handleCellSwitch(DynamicGridView.java:649) 11-05 02:36:37.310: E/AndroidRuntime(1132): at org.askerov.dynamicgrid.DynamicGridView.onTouchEvent(DynamicGridView.java:429) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.View.dispatchTouchEvent(View.java:7127) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919) 11-05 02:36:37.310: E/AndroidRuntime(1132): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)

ghost commented 9 years ago

Here is the modified code of GridActivity.java

public class GridActivity extends Activity {

private static final String TAG = GridActivity.class.getName();

private DynamicGridView gridView;
private ArrayList<Integer> imageIdList;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid);
   setUpList();
    gridView = (DynamicGridView) findViewById(R.id.dynamic_grid);
    gridView.setWobbleInEditMode(false);

   // gridView.setAdapter(new CheeseDynamicAdapter(this,new ArrayList<String>(Arrays.asList(Cheeses.sCheeseStrings)),2));
     gridView.setAdapter(new CheeseDynamicAdapter(this,imageIdList,2));

// add callback to stop edit mode if needed // gridView.setOnDropListener(new DynamicGridView.OnDropListener() // { // @Override // public void onActionDrop() // { // gridView.stopEditMode(); // } // }); gridView.setOnDragListener(new DynamicGridView.OnDragListener() { @Override public void onDragStarted(int position) { Log.d(TAG, "drag started at position " + position); }

        @Override
        public void onDragPositionsChanged(int oldPosition, int newPosition) {
            Log.d(TAG, String.format("drag item position changed from %d to %d", oldPosition, newPosition));
        }
    });
    gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            gridView.startEditMode(position);
            return true;
        }
    });

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(GridActivity.this, parent.getAdapter().getItem(position).toString(),
                    Toast.LENGTH_SHORT).show();
        }
    });
}

private void setUpList() {
    // TODO Auto-generated method stub
    imageIdList=new ArrayList<Integer>();
    imageIdList.add(R.drawable.dummy_one);
    imageIdList.add(R.drawable.dummy_two);
    imageIdList.add(R.drawable.dummy_three);
    imageIdList.add(R.drawable.dummy_four);
    imageIdList.add(R.drawable.dummy_one);
    imageIdList.add(R.drawable.dummy_two);
    imageIdList.add(R.drawable.dummy_three);
    imageIdList.add(R.drawable.dummy_four);
    imageIdList.add(R.drawable.dummy_one);
    imageIdList.add(R.drawable.dummy_two);
    imageIdList.add(R.drawable.dummy_three);
    imageIdList.add(R.drawable.dummy_four);
    imageIdList.add(R.drawable.dummy_one);
    imageIdList.add(R.drawable.dummy_two);
    imageIdList.add(R.drawable.dummy_three);
    imageIdList.add(R.drawable.dummy_four);
}

@Override
public void onBackPressed() {
    if (gridView.isEditMode()) {
        gridView.stopEditMode();
    } else {
        super.onBackPressed();
    }
}

}

And Here is the code of Adapter public class CheeseDynamicAdapter extends BaseDynamicGridAdapter { public CheeseDynamicAdapter(Context context, List<?> items, int columnCount) { super(context, items, columnCount); }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    CheeseViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_grid, null);
        holder = new CheeseViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (CheeseViewHolder) convertView.getTag();
    }
   holder.image.setImageResource((Integer) getItem(position));
    return convertView;
}

private class CheeseViewHolder {

    private ImageView image;

    private CheeseViewHolder(View view) {

        image = (ImageView) view.findViewById(R.id.item_img);
    }

}

}

vabhatia commented 9 years ago

I am also getting the same Null pointer exception on drag and drop till the bottom.