Noel Bautista
Noel Bautista

Reputation: 175

NullPointerException by called setAdapter in runnable in activity

Note: This piece of code works on a fragment in my app.. Same adapter but has getActivity().getApplicationContext() instead of Activity.this.

I'm trying to do the same thing in an activity but I keep getting a NPE on the setAdapter line. Assume the Arraylist item is populated by a different thread.

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //test if item Arraylist has stuff in it
                        for (MainGridItem t: item) {
                            System.out.println(t.getUrl()+t.getName()+t.getLinkUrl());
                        }

                        SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(new GridViewAdapter(GridViewActivity.this, item));
                        swingBottomInAnimationAdapter.setAbsListView(activity_grid);

                        assert swingBottomInAnimationAdapter.getViewAnimator() != null;
                        swingBottomInAnimationAdapter.getViewAnimator().setInitialDelayMillis(DELAY);

                        activity_grid.setAdapter(swingBottomInAnimationAdapter);

Upvotes: 0

Views: 135

Answers (3)

Noel Bautista
Noel Bautista

Reputation: 175

Ugh, I've been working on this for so long today that I didn't see that activity_grid wasn't injected. I apologize for the dumb question and thanks for the tips. I thought for the longest time that it was my adapter or some thread stuff I didn't know.

Upvotes: 0

IshRoid
IshRoid

Reputation: 3735

Not related to answer but It is just knowledge sharing and to resolve worse memory management mistakes

Avoid dependency injection frameworks

As Google official site says.

https://developer.android.com/training/articles/memory.html#DependencyInjection

Upvotes: 2

Sudarshan
Sudarshan

Reputation: 1291

@meowkittycat

With your code snippet i can say the NPE on the "activity_grid" object, check whether you intialized the Object in the Activity.

Upvotes: 1

Related Questions