Error : NullPointerException
can't get data in onPostExecute();
我在服务的onStartCommand()
中创建了一个AsyncTask;
启动后服务:
1。 LoadDataFromNetwork.getData
在doInBackground
中运行,此方法将从net获取数据,它可以正常工作,我可以看到Log;
2。我无法在onPostExcute();
中获取数据
为什么呢?
我是一个新人,有人可以帮助我吗?感谢先进!
4。代码:
enter code here 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;
}
}
答案 0 :(得分:0)
我认为问题出在LoadDataFromNetwork类的getData()方法中。 你能展示一下getData()方法吗?您是否以该方法初始化了List?
答案 1 :(得分:0)
enter code here public static List<ChannelBeans> getData () {
Wilddog wilddog = new Wilddog(url);
wilddog.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
map = (Map<String, Object>) dataSnapshot.getValue();
mDataList = getJsonDateFromMap(map);
Log.d(Application.TAG , "load_completed" +mDataList.toString());
}
@Override
public void onCancelled(WilddogError wilddogError) {
Log.d(Application.TAG , "Request Error");
}
});
return mDataList;
}