Consider:
Error : NullPointerException
can't get data in onPostExecute();
I create an AsyncTask in onStartCommand()
of a Service;
After startService:
LoadDataFromNetwork.getData
runs in doInBackground
. This method will get data from the Internet. It can be work, and I can see the Log;
I can't get data in onPostExcute();
Why?
Error image
Code:
public class Service_LoadAndStoreData extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(Application.TAG , "service start");
new AsyncTask<Void ,Void ,List<ChannelBeans>>(){
@Override
protected List<ChannelBeans> doInBackground(Void... params) {
return LoadDataFromNetwork.getData();//it can be work
}
@Override
protected void onPostExecute(List<ChannelBeans> channelBeanses) {
super.onPostExecute(channelBeanses);
Log.d(Application.TAG , channelBeanses.toString()); //Error :NullPointerException;
}
}.execute();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
The problem is at the getData() method from LoadDataFromNetwork class, I think.