Android Arraylist在异步方法中返回null?

Android Arraylist在异步方法中返回null?,android,arraylist,android-asynctask,Android,Arraylist,Android Asynctask,我面临的问题是,当在自定义适配器中调用ArrayList时,保存在ArrayList中的服务器的JSON数据返回null。自定义适配器用于在android中列出微调器中的数据 public class PhotoCommnFragment extends android.support.v4.app.Fragment { EditText rechargeMobileNumber,rechargeAmount; Spinner selectMenu; int flags[] = {R.drawa

我面临的问题是,当在自定义适配器中调用ArrayList时,保存在ArrayList中的服务器的JSON数据返回null。自定义适配器用于在android中列出微调器中的数据

public class PhotoCommnFragment extends android.support.v4.app.Fragment {
EditText rechargeMobileNumber,rechargeAmount;
Spinner selectMenu;
int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon};
List<SpinnerMenu> selectedNetwork = new ArrayList<>();
public PhotoCommnFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false);
    mobileRecahrgeHistory();
    rechargeMobileNumber = (EditText)rootView.findViewById(R.id.recharge_mobile_number);
    rechargeAmount = (EditText)rootView.findViewById(R.id.recharge_amount);
    selectMenu = (Spinner)rootView.findViewById(R.id.selectNetwork);
    settingSpinnerDropDown();
    return rootView;
}

public void mobileRecahrgeHistory(){
    Ion.with(this)
            .load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
            .asJsonObject().withResponse()
            .setCallback(new FutureCallback<Response<JsonObject>>() {
                @Override
                public void onCompleted(Exception e, Response<JsonObject> result) {
                    JSONObject json = null;

                    try {
                        json = new JSONObject(result.getResult().toString());
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    // Create the root JSONObject from the JSON string.
                    JSONObject jsonRootObject = null;
                    jsonRootObject = json.optJSONObject("DS");

                    //Get the instance of JSONArray that contains JSONObjects
                    JSONArray jsonArray = jsonRootObject.optJSONArray("LST");

                    //Iterate the jsonArray and print the info of JSONObjects
                    for(int i=0; i < jsonArray.length(); i++){
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = jsonArray.getJSONObject(i);
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        String iph = null;

                        String oid = jsonObject.optString("OID").toString();
                        String ocd = jsonObject.optString("OCD").toString();
                        String opd = jsonObject.optString("OPE").toString();
                        String mil = jsonObject.optString("MIL").toString();
                        String mxl = jsonObject.optString("MXL").toString();
                        try {
                             iph = jsonObject.getString("IPH").toString();
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }

                        String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph;
                        Bitmap mIcon11 = null;
                        try {
                            InputStream in = new java.net.URL(urldisplay).openStream();
                            mIcon11 = BitmapFactory.decodeStream(in);
                        } catch (Exception e3) {
                            e3.printStackTrace();
                        }
                        SpinnerMenu spinnerData = new SpinnerMenu();
                        spinnerData.setOid(oid);
                        spinnerData.setOcd(ocd);
                        spinnerData.setOpd(opd);
                        spinnerData.setMil(mil);
                        spinnerData.setMix(mxl);
                        spinnerData.setImage(mIcon11);

                        selectedNetwork.add(spinnerData);

                    }

                }

            });
}


public void settingSpinnerDropDown(){
    Fragment_DTH_Main_Spinner_Adapter customAdapter=new Fragment_DTH_Main_Spinner_Adapter(getActivity(),R.layout.fragment_dth_main_spinner_items,R.id.serviceName,selectedNetwork);
    selectMenu.setAdapter(customAdapter);
}

将customadapter设置为全局,并在oncomplete中调用notifydatasetchanged

public class PhotoCommnFragment extends android.support.v4.app.Fragment {
    EditText rechargeMobileNumber, rechargeAmount;
    Spinner selectMenu;
    int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon};
    List<SpinnerMenu> selectedNetwork = new ArrayList<>();
    Fragment_DTH_Main_Spinner_Adapter customAdapter;

    public PhotoCommnFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false);
        mobileRecahrgeHistory();
        rechargeMobileNumber = (EditText) rootView.findViewById(R.id.recharge_mobile_number);
        rechargeAmount = (EditText) rootView.findViewById(R.id.recharge_amount);
        selectMenu = (Spinner) rootView.findViewById(R.id.selectNetwork);
        settingSpinnerDropDown();
        customAdapter = new Fragment_DTH_Main_Spinner_Adapter(getActivity(), R.layout.fragment_dth_main_spinner_items, R.id.serviceName, selectedNetwork);
        return rootView;
    }

    public void mobileRecahrgeHistory() {
        Ion.with(this)
                .load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
                .asJsonObject().withResponse()
                .setCallback(new FutureCallback<Response<JsonObject>>() {
                    @Override
                    public void onCompleted(Exception e, Response<JsonObject> result) {
                        JSONObject json = null;

                        try {
                            json = new JSONObject(result.getResult().toString());
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        // Create the root JSONObject from the JSON string.
                        JSONObject jsonRootObject = null;
                        jsonRootObject = json.optJSONObject("DS");

                        //Get the instance of JSONArray that contains JSONObjects
                        JSONArray jsonArray = jsonRootObject.optJSONArray("LST");

                        //Iterate the jsonArray and print the info of JSONObjects
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = null;
                            try {
                                jsonObject = jsonArray.getJSONObject(i);
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                            String iph = null;

                            String oid = jsonObject.optString("OID").toString();
                            String ocd = jsonObject.optString("OCD").toString();
                            String opd = jsonObject.optString("OPE").toString();
                            String mil = jsonObject.optString("MIL").toString();
                            String mxl = jsonObject.optString("MXL").toString();
                            try {
                                iph = jsonObject.getString("IPH").toString();
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }

                            String urldisplay = "http://192.168.1.105/TotalRecharge/" + iph;
                            Bitmap mIcon11 = null;
                            try {
                                InputStream in = new java.net.URL(urldisplay).openStream();
                                mIcon11 = BitmapFactory.decodeStream(in);
                            } catch (Exception e3) {
                                e3.printStackTrace();
                            }
                            SpinnerMenu spinnerData = new SpinnerMenu();
                            spinnerData.setOid(oid);
                            spinnerData.setOcd(ocd);
                            spinnerData.setOpd(opd);
                            spinnerData.setMil(mil);
                            spinnerData.setMix(mxl);
                            spinnerData.setImage(mIcon11);

                            selectedNetwork.add(spinnerData);
                            customAdapter.notifyDataSetChanged();

                        }

                    }

                });
    }


    public void settingSpinnerDropDown() {

        selectMenu.setAdapter(customAdapter);
    }
公共类光通信片段扩展了android.support.v4.app.Fragment{
EditText rechargeMobileNumber,rechargeAmount;
微调器选择菜单;
int flags[]={R.drawable.airteltv、R.drawable.aircel、R.drawable.dishtv、R.drawable.sundirect、R.drawable.tatasky、R.drawable.videocon};
List selectedNetwork=newarraylist();
片段\ DTH \主\微调器\适配器自定义适配器;
公共光通信(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u tab\u mobile,container,false);
mobileRecahrgeHistory();
rechargeMobileNumber=(EditText)rootView.findViewById(R.id.RechargeU移动电话号码);
rechargeAmount=(EditText)rootView.findViewById(R.id.rechargeum\u amount);
selectMenu=(微调器)rootView.findviewbyd(R.id.selectNetwork);
设置喷丝头下拉菜单();
customAdapter=new Fragment\u DTH\u Main\u Spinner\u Adapter(getActivity(),R.layout.Fragment\u DTH\u Main\u Spinner\u items,R.id.serviceName,selectedNetwork);
返回rootView;
}
公共无效移动记录(){
(这个)离子
.加载(“http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
.asJsonObject().withResponse())
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(异常e,响应结果){
JSONObject json=null;
试一试{
json=新的JSONObject(result.getResult().toString());
}捕获(JSONException e1){
e1.printStackTrace();
}
//从JSON字符串创建根JSONObject。
JSONObject jsonRootObject=null;
jsonRootObject=json.optJSONObject(“DS”);
//获取包含JSONObject的JSONArray实例
JSONArray JSONArray=jsonRootObject.optJSONArray(“LST”);
//迭代jsonArray并打印JSONObject的信息
for(int i=0;i
自定义适配器在异步方法之后初始化,但适配器。notifyDataSetChanged()在异步方法内被调用。这会导致空指针异常吗?这会/不应该导致空指针。请重试,直到问题未解决,其返回空值
public class PhotoCommnFragment extends android.support.v4.app.Fragment {
    EditText rechargeMobileNumber, rechargeAmount;
    Spinner selectMenu;
    int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon};
    List<SpinnerMenu> selectedNetwork = new ArrayList<>();
    Fragment_DTH_Main_Spinner_Adapter customAdapter;

    public PhotoCommnFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false);
        mobileRecahrgeHistory();
        rechargeMobileNumber = (EditText) rootView.findViewById(R.id.recharge_mobile_number);
        rechargeAmount = (EditText) rootView.findViewById(R.id.recharge_amount);
        selectMenu = (Spinner) rootView.findViewById(R.id.selectNetwork);
        settingSpinnerDropDown();
        customAdapter = new Fragment_DTH_Main_Spinner_Adapter(getActivity(), R.layout.fragment_dth_main_spinner_items, R.id.serviceName, selectedNetwork);
        return rootView;
    }

    public void mobileRecahrgeHistory() {
        Ion.with(this)
                .load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
                .asJsonObject().withResponse()
                .setCallback(new FutureCallback<Response<JsonObject>>() {
                    @Override
                    public void onCompleted(Exception e, Response<JsonObject> result) {
                        JSONObject json = null;

                        try {
                            json = new JSONObject(result.getResult().toString());
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        // Create the root JSONObject from the JSON string.
                        JSONObject jsonRootObject = null;
                        jsonRootObject = json.optJSONObject("DS");

                        //Get the instance of JSONArray that contains JSONObjects
                        JSONArray jsonArray = jsonRootObject.optJSONArray("LST");

                        //Iterate the jsonArray and print the info of JSONObjects
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = null;
                            try {
                                jsonObject = jsonArray.getJSONObject(i);
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                            String iph = null;

                            String oid = jsonObject.optString("OID").toString();
                            String ocd = jsonObject.optString("OCD").toString();
                            String opd = jsonObject.optString("OPE").toString();
                            String mil = jsonObject.optString("MIL").toString();
                            String mxl = jsonObject.optString("MXL").toString();
                            try {
                                iph = jsonObject.getString("IPH").toString();
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }

                            String urldisplay = "http://192.168.1.105/TotalRecharge/" + iph;
                            Bitmap mIcon11 = null;
                            try {
                                InputStream in = new java.net.URL(urldisplay).openStream();
                                mIcon11 = BitmapFactory.decodeStream(in);
                            } catch (Exception e3) {
                                e3.printStackTrace();
                            }
                            SpinnerMenu spinnerData = new SpinnerMenu();
                            spinnerData.setOid(oid);
                            spinnerData.setOcd(ocd);
                            spinnerData.setOpd(opd);
                            spinnerData.setMil(mil);
                            spinnerData.setMix(mxl);
                            spinnerData.setImage(mIcon11);

                            selectedNetwork.add(spinnerData);
                            customAdapter.notifyDataSetChanged();

                        }

                    }

                });
    }


    public void settingSpinnerDropDown() {

        selectMenu.setAdapter(customAdapter);
    }