java.lang.IndexOutOfBoundsException:Invalid index x, size is x - java

After 3 hours of try i decided to ask here and see if someone can provide me a solution for this error: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
here is my code
public class ParcelAdapter extends BaseAdapter{
private final Activity context;
ParcelPOJO.Data items=new ParcelPOJO.Data();
ParcelAdapter(Activity context, ParcelPOJO.Data items){
this.context=context;
this.items=items;
}
#Override
public int getCount() {
return items.getLive().size();
}
#Override
public Object getItem(int position) {
return 0;
}
#Override
public long getItemId(int position) {
return 0;
}
class ViewHolder {
protected TextView eventCode;
protected TextView eventStatus;
protected TextView eventPrice;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
if(convertView==null) {
convertView = inflater.inflate(R.layout.parcel_row, null);
}
final ViewHolder viewHolder = new ViewHolder();
viewHolder.eventCode = (TextView) convertView.findViewById(R.id.pPackage_code);
viewHolder.eventStatus = (TextView) convertView.findViewById(R.id.pPackedStatus);
viewHolder.eventPrice =(TextView)convertView.findViewById(R.id.pPrice);
convertView.setTag(viewHolder);
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.eventCode.setText(items.getLive().get(position).getCodeNumber());
holder.eventStatus.setText(items.getLive().get(position).getStatus());
holder.eventPrice.setText(items.getLive().get(position).getOrderStorageShowPrice());
holder.eventCode.setText(items.getProcessing().get(position).getCodeNumber());
holder.eventStatus.setText(items.getProcessing().get(position).getStatus());
holder.eventPrice.setText(items.getProcessing().get(position).getOrderStorageShowPrice());
holder.eventCode.setText(items.getPacked().get(position).getCodeNumber());
holder.eventStatus.setText(items.getPacked().get(position).getStatus());
holder.eventPrice.setText(items.getPacked().get(position).getOrderStorageShowPrice());
//line 119 holder.eventCode.setText(items.getSent().get(position).getCodeNumber());
holder.eventStatus.setText(items.getSent().get(position).getStatus());
holder.eventPrice.setText(items.getSent().get(position).getOrderStorageShowPrice());
holder.eventCode.setText(items.getReceived().get(position).getCodeNumber());
holder.eventStatus.setText(items.getReceived().get(position).getStatus());
holder.eventPrice.setText(items.getReceived().get(position).getOrderStorageShowPrice());
return convertView;
}
}
logcat
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at dedmd.dedmd.fragmentsparcel.Parcels$ParcelAdapter.getView(Parcels.java:119)
at android.widget.AbsListView.obtainView(AbsListView.java:2267)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1156)
at android.view.View.measure(View.java:15172)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
at android.view.View.measure(View.java:15172)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:762)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
at android.view.View.measure(View.java:15172)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
if i delete this line i get the same error but with Invalid index 1, size is 1

This method tells the adapter how many items there are:
#Override
public int getCount() {
return items.getLive().size();
}
so apparently the array items.getSent() is not as long as the array items.getLive() - so the Adapter tries to go to a position after the end of it and it is invalid.
EDIT:
In other words, your items object has several arrays. One of them is items.getLive() and another one is items.getSent()
If you construct the "items" object and those two arrays have different lengths, then you will have problems. So, you need to fix how the items object is constructed before you construct your Adapter
EDIT 2:
If you do not control the source of your data, then you need either:
You need to look at your object contract (API) or get the source to create one. Having an object with sets of variable length arrays may not be what the provider intends and you have found a bug.
If you don't have an API or it states that you should expect possible null values, then you should be prepared for them. Maybe like this:
if (items.getSent() != null && items.getSent(),get(poistion)) {
holder.eventCode.setText(items.getSent().get(position).getCodeNumber());
...
}

Related

Android - How to make an intent work from inside a ListView using a custom ArrayAdapter

I have this problem that whenever i click on the listView OnItemClick method it always crashes when it is trying to reach another intent down below i leave some code.
public class SelectFichas extends AppCompatActivity {
TextView tv;
ListView lv;
RecyclerView.LayoutManager mLayoutManager;
ImageButton btn1;
ArrayList<Ficha> slFichas;
List<Ficha> ftchArray;
DBHelper db;
MyAdapter aAdapt = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_ficha);
btn1 = (ImageButton) (findViewById(R.id.rg));
lv = (ListView) (findViewById(R.id.lv1));
db = new DBHelper(this);
try {
//Verificamos que la base de datos no este vacia
if (!db.isEmpty()) {
ftchArray = db.getAllFichas();
slFichas = new ArrayList<Ficha>();
for (Ficha fi : ftchArray) {
slFichas.add(fi);
}
// mAdapter = new MyAdapter(ftchArray,getApplication());
aAdapt = new MyAdapter(SelectFichas.this,0,slFichas);
View header = getLayoutInflater().inflate(R.layout.list_header, null);
lv.addHeaderView(header);
lv.setAdapter(aAdapt);
lv.setItemsCanFocus(false);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id) {
Ficha fi = (Ficha) aAdapt.getItem(position-1);
String sel = fi.getPreg();
String[] opc = {"Actualizar", "Borrar", "Adivinar", "Atras"};
Toast.makeText(getApplicationContext(), "Usted selecciono " +sel, Toast.LENGTH_SHORT).show();
final Intent intent = new Intent(SelectFichas.this,UpdFicha.class);
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
if (intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
}
}, 2000);
I also used a custom ArrayAdapter for my ListView is that influencing, in case i will leave some code about the adapter below.
public class MyAdapter extends ArrayAdapter<Ficha> {
private Context context;
private ArrayList<Ficha> ficha;
public MyAdapter(Context context, int resource, ArrayList<Ficha> objects) {
super(context, resource, objects);
this.context = context;
this.ficha = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
Ficha fi = ficha.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_items, parent,false);
RelativeLayout rv = (RelativeLayout)(view.findViewById(R.id.relativeLayout));
TextView id = (TextView)view.findViewById(R.id.id_view);
TextView preg = (TextView)view.findViewById(R.id.preg_view);
TextView ans = (TextView) view.findViewById(R.id.answer_view);
id.setText(String.valueOf(fi.getId()));
preg.setText(fi.getPreg());
ans.setText(fi.getRes());
return view;
}
}
Here is my Logcat EDITED
2-05 11:52:06.886 5997-5997/com.giamp.fichas W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x9ccbcb20)
12-05 11:52:06.886 5997-5997/com.giamp.fichas E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.giamp.fichas, PID: 5997
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
at android.widget.Spinner.onMeasure(Spinner.java:482)
at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:416)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193) 
at android.widget.Spinner.onMeasure(Spinner.java:482) 
at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:416) 
at android.view.View.measure(View.java:16497) 
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719) 
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
at android.view.Choreographer.doFrame(Choreographer.java:544) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5017) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515)
AdapterView<?> parent is not an Adapter, but it is a ViewGroup.
change
ArrayAdapter<Ficha> aAdapt = new MyAdapter(SelectFichas.this, 0, slFichas);
to
final MyAdapter aAdapt = new MyAdapter(SelectFichas.this, 0, slFichas);
and
Ficha fi = (Ficha) parent.getItemAtPosition(position);
to
Ficha fi = (Ficha) aAdapt.getItem(position);

setAdapter() crashes app

When I call .notifyDataSetChanged() in my constructor after I set my ArrayList my app still crashes, the same as when I call it before I call .setAdapter, it's been bothering me for hours, would love any kind of help, thanks!
public class CustomSwipeAdapter extends PagerAdapter {
private ArrayList<String> image_resources = new ArrayList<>();
private Context mContext;
private LayoutInflater mLayoutInflater;
private ImageLoader mImageLoader;
public CustomSwipeAdapter(Context context, ArrayList<String> images){
this.mContext = context;
this.image_resources = images;
}
#Override
public int getCount() {
return image_resources.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
mLayoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
View item_view = mLayoutInflater.inflate(R.layout.swipe_layout, container, false);
final ImageView imageView = (ImageView) item_view.findViewById(R.id.imageView);
if (image_resources.get(position) != null || !image_resources.get(position).isEmpty()) {
mImageLoader.get(image_resources.get(position), new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
imageView.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
imageView.setBackgroundColor(Color.CYAN);
}
});
}
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
super.destroyItem(container, position, object);
}
}
Where I set my Adapter (in my Activity's onCreate()), it's most basic activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page);
mViewPager = (ViewPager) findViewById(R.id.view_pager);
mVolleySingleton = VolleySingleton.getInstance();
mRequestQueue = mVolleySingleton.getRequestQueue();
sendAPIRequest();
//after you get the images
mCustomSwipeAdapter = new CustomSwipeAdapter(this, images);
mViewPager.setAdapter(mCustomSwipeAdapter);
}
Stack Trace:
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 4 Pager id: sobmad.com.gamingreminder:id/view_pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class sobmad.com.gamingreminder.GamePage.CustomSwipeAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:967)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:15848)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
mCustomSwipeAdapter = new CustomSwipeAdapter(this, images);
mViewPager.setAdapter(mCustomSwipeAdapter);
mCustomSwipeAdapter.notifyDataSetChange() //Add this line in your activity

android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

There are other related issues on this but they do not address my situation (their error code has to do with some sort of recycling and/or bad cast calls by client code).
My situation is more complex.
I have some code where the user can pull up a library of photos.
The thing is, its working just fine on 7 phones that I have, all running API's 19+.
However I have a Google Nexus 4.3 that's running API 17. And I get this crash log which has none of my code, only library code. If you can advise how I might be able to code a work around I'd be all ears:
FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.GridView.onMeasure(GridView.java:1042)
at android.view.View.measure(View.java:15848)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Update
My offending class is this:
//Special class that works with the UIL
//The holder gets re-used and gets the new properties from this
static class ReusableGridViewView extends FrameLayout
{
public ImageView imageView;
public ProgressBar progressBar;
public ReusableGridViewView(Context context)
{
super(context);
setLayoutParams(new LayoutParams((int)(0.325* ViewHelper.screenWidthPX(context)),(int)(0.325* ViewHelper.screenWidthPX(context))));
imageView = new ImageView(context);
progressBar = new ProgressBar(context);
addView(imageView);
addView(progressBar);
}
}
Im using the Ultimate Image Loader library and this is the code that uses the above class.
//I do NOT call this directly, the listview decides when re use a convert view and passes it accordingly
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View view = convertView;
if (view == null)
{
view = new ReusableGridViewView(parent.getContext());
holder = new ViewHolder();
assert view != null;
holder.imageView = ((ReusableGridViewView)view).imageView;
holder.progressBar = ((ReusableGridViewView)view).progressBar;
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
//System.out.println(" " + holder + " ||| " + holder.imageView);
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener()
{
#Override
public void onLoadingStarted(String imageUri, View view)
{
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason)
{
holder.progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener()
{
#Override
public void onProgressUpdate(String imageUri, View view, int current, int total)
{
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
//set all as unselected
((ReusableGridViewView) view).removeSelectionBorder();
for(int i = 0; i < SELECTED_IMAGES.length; i++)
{
if(SELECTED_IMAGES[i] == position)
{
//set cell to selected
((ReusableGridViewView) view).addSelectionBorder();
}
}
return view;
}
So it turns out that on API < 17 FrameLayout can't be cast correctly to AbsListView params.
What I did was set AbsListView LayoutParams instead of FrameLayout params:
setLayoutParams(new AbsListView.LayoutParams(100,100));
I'm not sure if this is going to help you or what, but this crash used to happen for me once I was using AndroidSlidingUpPanel and adding a view inside of its content layout programmatically. After searching and trying many different solutions I came up with this solution; I added the following layout as the first child of the content layout and the crash was solved.
<android.widget.AbsListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent" />

GridView crash on android api level 14 although manifest is set for api 11 and up

I am testing my app. It works fine on my Samsung Galaxy S5 and on my LG with android 4.1.2. But when I try to test it on LG 4.0.4 (real device) or an a simulator of Nexus 5, it crashes due to the following stack trace. My manifest is set to minimum sdk 11. Below the stack trace, I am also showing my GridView code. Thanks for any help troubleshooting this.
Stack Trace
FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.GridView.onMeasure(GridView.java:1028)
at android.view.View.measure(View.java:12603)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12603)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12603)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
at android.view.View.measure(View.java:12603)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
GridView
Besides the usual findViewById, the only particular code in the activity that contains the GridView is
myGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressLint("NewApi")
#Override
public void onGlobalLayout() {
if (myGridViewAdapter.getNumColumns() == 0) {
final int numColumns = 2;
if (numColumns > 0) {
final int columnWidth = myGridView.getWidth() / numColumns;// - mImageThumbSpacing;
myGridViewAdapter.setNumColumns(numColumns);
myGridViewAdapter.setItemHeight(columnWidth);
if (BuildConfig.DEBUG) {
Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
}
if (Utils.hasJellyBean()) {
myGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
myGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}
}
});
Then inside the adapter in the method getView, I have this snippet
public View getView(int position, View convertView, ViewGroup parent) {
// First check if this is the top row
if (position < mNumColumns) {
if (convertView == null) {
convertView = new View(mContext);
}
// Set empty view with height of ActionBar
convertView
.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mActionBarHeight));
return convertView;
}
Try
convertView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, mActionBarHeight));

lazylist adapter issue while scrolling

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View vi= convertView;
if(convertView==null){
viewHolder = new ViewHolder();
vi = inflater.inflate(R.layout.list_row, null);
viewHolder.content = (TextView)vi.findViewById(R.id.f_content);
viewHolder.cat_id = (TextView)vi.findViewById(R.id.c_id);
viewHolder.fact_id = (TextView)vi.findViewById(R.id.f_id);
vi.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> fact = new HashMap<String, String>();
fact = data.get(position);
// Setting all values in listview
viewHolder.fact_id.setText(fact.get("_id"));
viewHolder.cat_id.setText(fact.get("cat_id"));
viewHolder.content.setText(fact.get("fact_content"));
return vi;
}
I have created a LazyAdapter that extends a BaseAdaper to use with ListView.
The this adapter populates data in ListView. When I click on first 4 visible list items it prints the value of that item.
Problem is when I scroll down the list view and click on item 5. The app crashes saying null pointer exception. Log trace is here for reference.
05-13 23:10:43.943: E/AndroidRuntime(3360): FATAL EXCEPTION: main
05-13 23:10:43.943: E/AndroidRuntime(3360): java.lang.NullPointerException
05-13 23:10:43.943: E/AndroidRuntime(3360): at com.example.facts.MainActivity$3.onItemClick(MainActivity.java:100)
05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.ListView.performItemClick(ListView.java:3569)
05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Handler.handleCallback(Handler.java:587)
05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Looper.loop(Looper.java:150)
05-13 23:10:43.943: E/AndroidRuntime(3360): at android.app.ActivityThread.main(ActivityThread.java:4389)
05-13 23:10:43.943: E/AndroidRuntime(3360): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 23:10:43.943: E/AndroidRuntime(3360): at java.lang.reflect.Method.invoke(Method.java:507)
05-13 23:10:43.943: E/AndroidRuntime(3360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
05-13 23:10:43.943: E/AndroidRuntime(3360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
05-13 23:10:43.943: E/AndroidRuntime(3360): at dalvik.system.NativeStart.main(Native Method)
The Onclick Methods is like this:
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
RelativeLayout node = (RelativeLayout)parent.getChildAt(position);
TextView fact = (TextView) node.findViewById(R.id.c_id);
Log.d("ListItem Clicked", "id: " + id + " position: "
+ position + " artist " + fact.getText());
}
});
My suggestion would be to revisit your onItemClickListener implementation.
You should not need to access parent at all for what you are trying to do. Try this.
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView fact = (TextView) view.findViewById(R.id.c_id);
Log.d("ListItem Clicked", "id: " + id + " position: "
+ position + " artist " + fact.getText());
}
});
The view argument supplied in the method is actually the view being click on.
view; The view within the AdapterView that was clicked (this will be a view provided by the adapter)
Please refer to AdapterView.OnItemClickListener for more details.
The parent is there to help you identified which AdapterView that is being acted on. For example, say if you have two ListAdapter but only want to use one listener then it going to help you specify which is the one.

Categories