plattysoft / grid-with-header-list-adapter

Library and example of how to display a grid view with a header using a normal list view
39 stars 20 forks source link

NullPointerException #2

Closed leonardojobim closed 10 years ago

leonardojobim commented 10 years ago

Hi,

I am getting a NullpointerException when I remove the data. I am avoiding it in this way:

@Override
protected View getItemView(int position, View convertView, ViewGroup parent) {
// ... code
if (holder != null) {
    Picasso.with(context)
        .load(photo.getUrl())
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.error)
        .into(holder.picture);
    }
}

It mitigates the problem, but it shows a strange list with empty items, like below (assuming three columns) :

X X X X X X
X X X X X X

X is the grid item ( a picture, in this case ) and there is blank space.

It occurs when I show the gridview in a fragment, and then go to other fragments and remove data. When I come back to fragment that showed the grid view, the Exception is called. The gridView and the adapter are always instantiated in onCreateView():

....
photoList = new ArrayList<Photo>(profile.getPhotoList());
adapter = new ProfileGridAdapter(getActivity(), photoList);
adapter.setNumColumns(3);
photosListView.setAdapter(adapter); 
....

Apparently there is no reason for this to occur.

Thank you for your attention.

Regards,

plattysoft commented 10 years ago

Hi, thanks for the mail.

Are you recreating the adapter after the data is removed? Or at least calling notifyDataSetChanged?

So, if I understand correctly, the view is not null, but the Tag inside it is null. Correct?

Will see if I can take a look.

A small sample project will help me replicate the problem to fix it ot seeing if there is something missing in your code.

Cheers. On 9 Jan 2014 04:53, "leo-one" notifications@github.com wrote:

Hi,

I am getting a NullpointerException when I remove the data. I am avoiding it in this way:

@Override https://github.com/Override protected View getItemView(int position, View convertView, ViewGroup parent) { // ... code if (holder != null) { Picasso.with(context) .load(photo.getUrl()) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .into(holder.picture); } }

It mitigates the problem, but it shows a strange list with empty items, like below (assuming three columns) :

X X X X X X

X X X X X X

X is the grid item ( a picture, in this case ) and there is blank space.

It occurs when I show the gridview in a fragment, and then go to other fragments and remove data. When I come back to fragment that showed the grid view, the Exception is called. The gridView and the adapter are always instantiated in onCreateView():

.... photoList = new ArrayList(profile.getPhotoList()); adapter = new ProfileGridAdapter(getActivity(), photoList); adapter.setNumColumns(3); photosListView.setAdapter(adapter); ....

Apparently there is no reason for this to occur.

Thank you for your attention.

Regards,

— Reply to this email directly or view it on GitHubhttps://github.com/plattysoft/grid-with-header-list-adapter/issues/2 .

leonardojobim commented 10 years ago

Hi,

I think that the problem is not related to the tag. If I do not use tag, I receive this error:

java.lang.ClassCastException: android.view.View cannot be cast to com.plattysoft.gridviewwithheaderexample.SquaredImageView
....
at com.plattysoft.ui.ListAsGridBaseAdapter.updateItemRow(ListAsGridBaseAdapter.java:144)

SquaredImageView is simply a subclass of ImageView.

I modified the sample project. It has a MainFragment, that show a ListView of images with the ListAsGridExampleAdapter. And a new fragment called PagerFragment, where the user can remove the images. This fragment is called when an item is clicked in MainFragment.

If the user remove some images and back to the MainFragment, it can launch an exception an ANR. It does not always occur. This non-deterministic behavior makes it so difficult to debug. But it has happened with enough frequency, probably in second or fourth time that the user returns to MainFragment.

And it occurs in scrolling event in the upward direction.

It is not allowed to attach the project here. I am sending by email, in github.reply. I do not know if the file will arrive or if it is an automatic response. If not, I can send to another email or make available in any public link.

It is a tar.gz file and can be import to Eclipse.

Thank you for replying and attention.

Regards,

plattysoft commented 10 years ago

Ok, so, the tag is null because the view not the one that should be.

Anytime the content of the list changes you should recreate the adapter, or at least call to notifyDataSetChanged. Are you doing that?

Will take a look at the sample to see if I find something obvious.

Cheers.

On Fri, Jan 10, 2014 at 2:41 AM, leo-one notifications@github.com wrote:

2014/1/9 Raul Portales notifications@github.com

Hi, thanks for the mail.

Are you recreating the adapter after the data is removed? Or at least calling notifyDataSetChanged?

So, if I understand correctly, the view is not null, but the Tag inside it is null. Correct?

Will see if I can take a look.

A small sample project will help me replicate the problem to fix it ot seeing if there is something missing in your code.

Cheers. On 9 Jan 2014 04:53, "leo-one" notifications@github.com wrote:

Hi,

I am getting a NullpointerException when I remove the data. I am avoiding it in this way:

@Override https://github.com/Override protected View getItemView(int position, View convertView, ViewGroup parent) { // ... code if (holder != null) { Picasso.with(context) .load(photo.getUrl()) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .into(holder.picture); } }

It mitigates the problem, but it shows a strange list with empty items, like below (assuming three columns) :

X X X X X X

X X X X X X

X is the grid item ( a picture, in this case ) and there is blank space.

It occurs when I show the gridview in a fragment, and then go to other fragments and remove data. When I come back to fragment that showed the grid view, the Exception is called. The gridView and the adapter are always instantiated in onCreateView():

.... photoList = new ArrayList(profile.getPhotoList()); adapter = new ProfileGridAdapter(getActivity(), photoList); adapter.setNumColumns(3); photosListView.setAdapter(adapter); ....

Apparently there is no reason for this to occur.

Thank you for your attention.

Regards,

— Reply to this email directly or view it on GitHub< https://github.com/plattysoft/grid-with-header-list-adapter/issues/2> .

— Reply to this email directly or view it on GitHub< https://github.com/plattysoft/grid-with-header-list-adapter/issues/2#issuecomment-31906691>

.

— Reply to this email directly or view it on GitHubhttps://github.com/plattysoft/grid-with-header-list-adapter/issues/2#issuecomment-31996036 .

Raul Portales Platty Soft http://plattysoft.com

leonardojobim commented 10 years ago

Hi,

I changed the code below from onCreateView() to onResume() and now the app apparently works fine.

        ListAsGridExampleAdapter mAdapter = new ListAsGridExampleAdapter(getActivity());
        mAdapter.setNumColumns(3);
        mAdapter.setBackgroundResource(R.drawable.row);
        mAdapter.setOnGridClickListener( new GridItemClickListener() {
                // More code
        });
        listView.setAdapter (mAdapter);

I thought that the adapter was recreated in onCreateView() because when fragment returns to foreground, it begins by this method, according the fragment lifecycle documentation: http://developer.android.com/images/fragment_lifecycle.png

The problem is solved. Thank you very much for attention and explanation.

Regards