片段

时间:2018-04-06 11:42:25

标签: android android-fragments nullpointerexception

编辑:是的,我有点重新发布了这个,但是如果你阅读了这两篇帖子,他们都指出了(希望)同样原因的不同问题。在我的另一个问题中,特定小部件为空。这里,整个getView函数返回null。

我这几个小时以来一直在努力。找不到任何理由。我不能在这里发布整个片段,但以下内容应该清楚。

@BindView(R.id.acService) AutoCompleteTextView autocompleteService;
@BindView(R.id.acAddress) AutoCompleteTextView autocompleteAddress;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    unbinder = ButterKnife.bind(this, view);

    initialize();
    loadSkillsData();

    return view;
}

private void initialize()
{
    context = getActivity();
    util = new Util(context);
    requestService = new RequestService();
    geoDataClient = Places.getGeoDataClient(context, null);

    autocompleteAdapter = new PlaceAutocompleteAdapter(context, geoDataClient, BOUNDS_ONTARIO, null);
    autocompleteAddress.setAdapter(autocompleteAdapter);

    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
    mapFragment.getMapAsync(this);
}

private void loadSkillsData()
{
    Realm realm = getRealm();
    UserModel user = realm.where(UserModel.class).findFirst();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RestAPI.ENDPOINT)
            .addConverterFactory(MoshiConverterFactory.create())
            .build();
    RestAPI restApi = retrofit.create(RestAPI.class);
    Call<ResponseSkills> loginCall = restApi.getSkills(user.getServerUserId());
    loginCall.enqueue(new Callback<ResponseSkills>()
    {
        @Override
        public void onResponse(Call<ResponseSkills> call, final Response<ResponseSkills> response)
        {
            if (response.isSuccessful())
            {
                if (response.body().getStatus())
                {
                    skillList = response.body().getSkillList();
                    ArrayAdapter<SkillModel> skillAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, skillList);
AutoCompleteTextView acService = getView().findViewById(R.id.acService);
                    acService.setAdapter(skillAdapter);
                }
                else
                {
                    switch (response.body().getError())
                    {
                        default:
                            Toasty.error(context, response.body().getError());
                            break;
                    }
                }
            } else
            {
                Toasty.error(context, getString(R.string.toast_experienced_a_problem)).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseSkills> call, Throwable t)
        {
            Toasty.error(context, getString(R.string.toast_experienced_a_problem)).show();
            t.printStackTrace();
        }
    });
}

布局:XML

AutoCompleteTextView acService = getView()。findViewById(R.id.acService); 一行上,我得到一个NPE,说getView返回null。

为什么会这样?

0 个答案:

没有答案