PlaceAutocompleteFragment无法在Android 5.0&截至

时间:2016-05-30 13:12:27

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

我想在我的应用程序中使用PlaceAutocompleteFragment

我成功实施PlaceAutocompleteFragment并且工作正常。(在三星S4和HTC Desire中)。

但是当我在 Android 5.0(Mi4i) Android 5.1.1(Samsung Galaxy Note 10.1) Android 6.0(Moto G Turbo)中检查此功能时)应用程序崩溃....

我在Fragment使用以下代码实施 PlaceSelectionListener

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(this);

@Override
    public void onPlaceSelected(Place place) {
        Toast.makeText(getActivity(), "Place selection : " + place.getAddress(),
                Toast.LENGTH_SHORT).show();
    }

    /**
     * Callback invoked when PlaceAutocompleteFragment encounters an error.
     */
    @Override
    public void onError(Status status) {

        Toast.makeText(getActivity(), "Place selection failed: " + status.getStatusMessage(),
                Toast.LENGTH_SHORT).show();
    }

在XML布局文件中。

<fragment
      android:id="@+id/autocomplete_fragment"
      android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />

logcat的

  

致命的例外:主要                                                                                   处理:com.organization.project,PID:11635                                                                                   java.lang.NullPointerException:尝试调用虚方法'void   com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)”   在null对象引用上                                                                                       在   com.organization.project.fragments.FragmentSearchPlace.onCreateView(FragmentSearchPlace.java:39)                                                                                       在android.app.Fragment.performCreateView(Fragment.java:2053)                                                                                       在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)                                                                                       在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)                                                                                       在android.app.BackStackRecord.run(BackStackRecord.java:839)                                                                                       在   android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)                                                                                       在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:447)                                                                                       在android.os.Handler.handleCallback(Handler.java:739)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                       在android.os.Looper.loop(Looper.java:135)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5234)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在java.lang.reflect.Method.invoke(Method.java:372)                                                                                       在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:909)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

在Gradle文件中添加了以下依赖项。

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.makeramen:roundedimageview:2.2.1'
compile "com.android.support:cardview-v7:23.1.1"
compile 'com.google.android.gms:play-services-maps:9.0.1'
compile 'com.google.android.gms:play-services-location:9.0.1'

2 个答案:

答案 0 :(得分:4)

从堆栈跟踪中,看起来您正在使用嵌套在另一个片段(FragmentSearchPlace)中的Autocomplete片段。也许使用getChildFragmentManager()代替getFragmentManager()会有效。

答案 1 :(得分:0)

您不会通过属性&#34; name&#34;将片段添加到其他片段中。在您的布局中。你必须使用:

autocompleteFragment = new SupportPlaceAutocompleteFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();           
transaction.replace(R.id.fragme_autocomplete_destino,autocompleteFragment);
transaction.commit();