首先,我想说我是这个“编程世界”的新手,但是我一直在努力应对Google Places的autocompleteFragment, 我正在尝试在我的应用程序中实现autocompleteFragment,但是在输入“欢迎活动”后,它将引发错误并粉碎应用程序:
Process: com.app.mk.transport, PID: 20295
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.mk.transport/com.app.mk.transport.Welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.libraries.places.widget.AutocompleteSupportFragment.setPlaceFields(java.util.List)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.libraries.places.widget.AutocompleteSupportFragment.setPlaceFields(java.util.List)' on a null object reference
at com.app.mk.transport.Welcome.setUpLocationPicker(Welcome.java:237)
at com.app.mk.transport.Welcome.onCreate(Welcome.java:216)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是我的代码:
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), String.valueOf(R.string.google_api_key));
}
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ADDRESS));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
if (location_switch.isChecked()) {
destination = place.getAddress();
destination = destination.replace(" ", "+");
getDirection();
}
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Toast.makeText(Welcome.this, "" + status.toString(), Toast.LENGTH_SHORT).show();
Log.i(TAG, "An error occurred: " + status);
}
});
我不确定我在做什么错,但是如果有人指出我的错误,我将非常感激。
如果我需要向您展示其他“代码”或其他内容,请告诉我是否丢失了它!
谢谢。
答案 0 :(得分:0)
嗨,这个问题是在片段中实现的。请使用 getChildFragmentManager 而不是 getSupportFragmentManager / getFragmentManger()。 这将完美地工作。就像这样:-
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), "YOUR_API_KEY");
}
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
这对我有用,希望对您也有用。谢谢。...
答案 1 :(得分:0)
如果您正在使用活动,请使用<androidx.fragment.app.FragmentContainerView>
代替< <fragment>
标签。
并添加实现“ androidx.fragment:fragment:1.2.0”依赖项。