更改PlaceAutoCompleteFragment的提示时发生NullPointerException

时间:2018-08-05 08:05:15

标签: android android-fragments google-places-api placeautocompletefragment

在我的一个片段中,我实现了一个PlaceAutocompleteFragment,如下所示:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    placeAutocompleteFragment = new SupportPlaceAutocompleteFragment();
    getChildFragmentManager().beginTransaction()
            .replace(R.id.place_autocomplete_fragment_container, placeAutocompleteFragment)
            .addToBackStack(SupportPlaceAutocompleteFragment.class.getName())
            .commit();

    EditText etPlace = (EditText) placeAutocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input);
    etPlace.setHint("Type your address");
}

最后两行是我尝试更改搜索栏中的提示文本。但是,当我添加它们时,会出现异常

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

我也尝试过placeAutocompleteFragment.setHint()。我应该如何去代替setHint()?预先感谢。

编辑:崩溃日志显示以下内容

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setHint(java.lang.CharSequence)' on a null object reference
    at me.user.appname.ExploreFragment.onCreateView(ExploreFragment.java:99)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2425)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2623)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2410)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2365)
    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2242)
    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:168)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
    at android.view.View.measure(View.java:23265)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:23265)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
    at android.view.View.measure(View.java:23265)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
    at android.view.View.measure(View.java:23265)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:23265)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
    at android.view.View.measure(View.java:23265)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:898)
    at android.view.View.measure(View.java:23265)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2832)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1869)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2124)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1738)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7745)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938) 08-05 01:26:32.231 28683-28683/me.user.appname E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

编辑#2:我的ExploreFragment的要点

public class ExploreFragment extends Fragment {
GeoDataClient mGeoDataClient;
SupportPlaceAutocompleteFragment placeAutocompleteFragment;
private OnFragmentInteractionListener mListener;

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

public static ExploreFragment newInstance(String param1, String param2) 
{
    ExploreFragment fragment = new ExploreFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    final View rootView = inflater.inflate(R.layout.fragment_explore,
            container, false);

    mGeoDataClient = Places.getGeoDataClient(getActivity());

    placeAutocompleteFragment = new SupportPlaceAutocompleteFragment();
    getChildFragmentManager().beginTransaction()
            .replace(R.id.place_autocomplete_fragment_container, placeAutocompleteFragment)
            .addToBackStack(SupportPlaceAutocompleteFragment.class.getName())
            .commit();
    EditText etPlace = (EditText) rootView.findViewById(R.id.place_autocomplete_search_input);
    etPlace.setHint("Type your address");
    return rootView;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onAttachFragment(Fragment childFragment) {
    super.onAttachFragment(childFragment);
    ((SupportPlaceAutocompleteFragment) childFragment).setOnPlaceSelectedListener(
            new PlaceSelectionListener() {
                @Override
                public void onPlaceSelected(Place place) {
                    // set place variables here
                }

                @Override
                public void onError(Status status) {
                    Log.i("OnPlaceSelected", "An error occurred: " + status);
                }
            }
    );
}


@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {
    // methods here
}

}

2 个答案:

答案 0 :(得分:0)

将以下代码移至placeAutocompleteFragment内的onCreateview

我已将placeAutocompleteFragment.getView()替换为view

EditText etPlace = (EditText) view.findViewById(R.id.place_autocomplete_search_input);
etPlace.setHint("Type your address");

答案 1 :(得分:0)

在您的第一个代码段中,您使用了:

EditText etPlace = (EditText) placeAutocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input);

但是在第二个片段中,您使用了

EditText etPlace = (EditText) rootView.findViewById(R.id.place_autocomplete_search_input);

您可以调试并确保您的etPlace编辑文本不为空吗?

使用SupportPlaceAutocompleteFragment时,应AppCompatActivity(或FragmentActivity并使用getSupportFragmentManager())来实现您的活动。

对于第二个选项,您可以创建一个PlaceAutoCompleteFragment而不是SupportPlaceAutoCompleteFragment