android


Null pointer in retrofit callback when call getActivity


I'm using retrofit for fetching data from resource. But think my architecture is wrong.
So, i have an fragment with listview, for example.
In onCreateView after UI setup i calls API method(async). That returns list of models i need to setup my listview adapter.
Thats i do in callback
private Callback<List<User>> mUsersCallback = new Callback<List<User>>() {
#Override
public void success(List<User> users, Response response) {
mLoadingLayout.hideLoading();
mPeopleAdapter = new PeopleAdapter(getActivity(), users);
lvPeople.setAdapter(mPeopleAdapter);
}
#Override
public void failure(RetrofitError error) {
mLoadingLayout.hideLoading();
Log.d("get users", error.getUrl() + " " + error.toString());
}
};
In this part i sometimes get NPE when call getActivity();
How to do it on right way?
Try to create your own Callback class and there pass your Activity or Context by constructor.
Similar to this answer: http://stackoverflow.com/a/25665521/2399340
Your activity has been destroyed during the call, when you try to get it in your callback, it may be null.
Simply check if activity is not null and if null ignore the callback.
Retrofit is complected i suggest you to try Okhttp , Volley , Ion

Related Links

SoftKeyBoard get list of words suggestion
My calculator project made in android studio doesn't scale
Android keep recyclerview recycling inside scrollview
Android Final variable error
Change package name for Android in React Native
show multiple messages in single notification
getLatitude and getLongitude crashing on Samsung Galaxy S6
Appcelerator PIcker Sizing & Hint Text Problems
Image recognition Android
How to solve Out of memory on a 8294416-byte allocation exception?
android google drive api v3 media_in_progress not working
ClassCastException from View objects in android studio
How to bypass proxy for a specific URL in Android emulator
how to convert unicode \u003c to correct web content and display correct content in android webview so what i should do for that
Run javascript code in android browser via an intent
How to draw a stroke on a half of a circle on top of the layout and the layout itself?

Categories

HOME
go
cygwin
porting
polymer
string
dns
apache-jena
ns-3
character-encoding
jmx
aws-cli
google-apps
mapbox
nuget-package
special-characters
openstreetmap
owin
pybossa
docx
dreamweaver
sar
php-mysqlidb
mongoid6
interpreter
moses
stackexchange
angularjs-components
underscore.js
genexus-sd
cppreference
winpe
sqsh
boost-asio
papaparse
slacktextviewcontroller
sharepoint-workflow
hivemq
openerp-7
flatmap
css-position
cin
kill
numberpicker
mcustomscrollbar
contract
plan-9
linter
robomongo
configurationmanager
rackspace-cloud
template-toolkit
mathcad
jtree
bootstrap-tags-input
juice-ui
cvxpy
atlas
lrs
myspace
omniauth-facebook
data-protection
turbo-prolog
directdraw
file-move
wiredep
forwarding
video-player
fuelcms
canalyzer
jdk1.7
sha512
liquid-layout
zxspectrum
mmwormhole
document-repository
beatsmusic
paw
buffering
gae-quotas
eclipse-kepler
isqlquery
superscrollorama
insert-id
wimax
boost-signals
appjs
pinch
denied
event-receiver
delimited-text
maven-1
dot-emacs
qtabbar
visualj#
int64
change-management
online-storage

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App