I have a gridview, in which i have 24 odd elements.
Ive given the grid a fixed height so scroller appears w.r.t the elements not displayed.
My issue is say given below is my gridview ,where the red line signifies the end of the visible grid and to see below the red line one has to scroll
say i select 6 then scroll and select 22 then again scroll up and select 11 my app crashes with the following error.
Exception is: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.adamas.ui.AddNewBuyEntryActivity$2.onItemClick(AddNewBuyEntryActivity.java:205)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
if i dont give my grid a fixed height and let it wrap without scroll my app doesnt crash in this scenario.
error occurs at
((ImageView) parent.getChildAt(previousSelectedShapePosition).findViewById(R.id.img_sales_shape))
.setImageResource(shapeIcons.getResourceId(previousSelectedShapePosition, -1));
code snippet where crash occurs[im using previous position and current position to toggle the colors of selected and previously selected elements]
grdShapes.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
previousSelectedShapePosition = currentSelectedShapePosition;
currentSelectedShapePosition = position;
if (spnShape.getSelectedItem().toString().equalsIgnoreCase("Polished")) {
((ImageView) view.findViewById(R.id.img_sales_shape)).setImageResource(shapeSelectedIcons.getResourceId(position, 1));
((ImageView) parent.getChildAt(previousSelectedShapePosition).findViewById(R.id.img_sales_shape))
.setImageResource(shapeIcons.getResourceId(previousSelectedShapePosition, -1));
} else {
((ImageView) view.findViewById(R.id.img_sales_shape)).setImageResource(roughShapeSelected.getResourceId(position, 1));
((ImageView) parent.getChildAt(previousSelectedShapePosition).findViewById(R.id.img_sales_shape))
.setImageResource(roughShapeIcons.getResourceId(previousSelectedShapePosition, -1));
}
}
});
UPDATE: Spinner code snippet on which basis adapter is set
spnShape.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View view, int position, long id) {
previousSelectedShapePosition = 0;
if (position == 0) {
roughORpolished = 0;
adapter = new GridShapeSalesAdapter(AddNewBuyEntryActivity.this, roughShapeItems,roughORpolished);
grdShapes.setAdapter(adapter);
} else if (position == 1) {
roughORpolished = 1;
adapter = new GridShapeSalesAdapter(AddNewBuyEntryActivity.this, polishedShapeItems,roughORpolished);
grdShapes.setAdapter(adapter);
}
}
here is my gridview adapter