通过 findFragmentById 访问时出现 PlaceAutoCompleteFragment NullPointerException
PlaceAutoCompleteFragment NullPointerException when accessing via findFragmentById
下面的 XML 显示我确实添加了 fragment
。
很多,但你可以在底部找到 fragment
。其他 UI 元素无关紧要,为了完整起见,我只是将它们包括在内。
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:columnCount="2"
android:rowCount="7">
<EditText
android:layout_gravity="fill_horizontal"
android:id="@+id/title"
android:layout_row="0"
android:layout_column="0"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:hint="Event name"
android:textSize="35dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center" />
<LinearLayout
android:layout_row="2"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:id="@+id/fromLabel"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="From:"
android:textSize="30dp"
android:layout_centerVertical="true"
/>
<EditText
android:id="@+id/fromDate"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2017.01.01"
android:textSize="20dp"
android:gravity="center"
android:clickable="true"
android:focusable="false"
/>
<EditText
android:text="00:00"
android:layout_weight="1"
android:textSize="20dp"
android:id="@+id/fromTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:clickable="true"
android:focusable="false"
/>
</LinearLayout>
<LinearLayout
android:layout_gravity="fill_horizontal"
android:layout_row="3"
android:layout_column="0"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:id="@+id/toLabel"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="To:"
android:textSize="30dp"
android:layout_centerVertical="true"
/>
<EditText
android:id="@+id/toDate"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2017.01.01"
android:textSize="20dp"
android:gravity="center"
android:clickable="true"
android:focusable="false"
/>
<EditText
android:text="00:00"
android:layout_weight="1"
android:textSize="20dp"
android:id="@+id/toTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:clickable="true"
android:focusable="false"
/>
</LinearLayout>
<EditText
android:layout_gravity="fill_horizontal"
android:id="@+id/description"
android:layout_row="4"
android:layout_column="0"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:hint="Description"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center" />
<Button
android:id="@+id/submitEvent"
android:layout_gravity="fill_horizontal"
android:layout_row="5"
android:layout_column="0"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Submit event"/>
<FrameLayout
android:layout_gravity="fill"
android:layout_row="6"
android:layout_rowSpan="1"
android:layout_column="0"
android:id="@+id/friendsList"
android:layout_width="0dp"
android:layout_columnWeight="0.6"
android:layout_height="0dp"
>
</FrameLayout>
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="7"
android:layout_column="1"
android:layout_width="0dp"
android:layout_columnWeight="1.6"
android:layout_height="0dp"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</GridLayout>
我有 Activity
文件,其 onCreare
包含此代码 AFTER setContentView(R.layout.activity_event);
PlaceAutocompleteFragment autocompleteFragment =
(PlaceAutocompleteFragment) getFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i("PLACE", place.getName().toString());
}
@Override
public void onError(Status status) {
Log.i("PLACE", "ERROR WHEN AUTO COMPLETE PLACE");
}
});
问题是,setOnPlaceSelectedListener
一直给我 NullPointerException
,并说我正在尝试将侦听器设置为 null
对象。我需要在片段中使用 Google 个位置。
请注意,对于较小的屏幕,Activity
版本可以正常启动。问题仅在于此。
您需要创建 class 来扩展片段,重写 onCreateView 方法并在该方法中扩展您的片段布局 (place_autocomplete_fragment)。然后创建该 class 的对象,然后才能设置侦听器。
class FragmentClass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.place_autocomplete_fragment, container, false);
return view;
}
}
然后创建 FragmentClass 对象并设置所需的侦听器。
下面的 XML 显示我确实添加了 fragment
。
很多,但你可以在底部找到 fragment
。其他 UI 元素无关紧要,为了完整起见,我只是将它们包括在内。
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:columnCount="2"
android:rowCount="7">
<EditText
android:layout_gravity="fill_horizontal"
android:id="@+id/title"
android:layout_row="0"
android:layout_column="0"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:hint="Event name"
android:textSize="35dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center" />
<LinearLayout
android:layout_row="2"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:id="@+id/fromLabel"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="From:"
android:textSize="30dp"
android:layout_centerVertical="true"
/>
<EditText
android:id="@+id/fromDate"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2017.01.01"
android:textSize="20dp"
android:gravity="center"
android:clickable="true"
android:focusable="false"
/>
<EditText
android:text="00:00"
android:layout_weight="1"
android:textSize="20dp"
android:id="@+id/fromTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:clickable="true"
android:focusable="false"
/>
</LinearLayout>
<LinearLayout
android:layout_gravity="fill_horizontal"
android:layout_row="3"
android:layout_column="0"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:id="@+id/toLabel"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="To:"
android:textSize="30dp"
android:layout_centerVertical="true"
/>
<EditText
android:id="@+id/toDate"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2017.01.01"
android:textSize="20dp"
android:gravity="center"
android:clickable="true"
android:focusable="false"
/>
<EditText
android:text="00:00"
android:layout_weight="1"
android:textSize="20dp"
android:id="@+id/toTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:clickable="true"
android:focusable="false"
/>
</LinearLayout>
<EditText
android:layout_gravity="fill_horizontal"
android:id="@+id/description"
android:layout_row="4"
android:layout_column="0"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:hint="Description"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center" />
<Button
android:id="@+id/submitEvent"
android:layout_gravity="fill_horizontal"
android:layout_row="5"
android:layout_column="0"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Submit event"/>
<FrameLayout
android:layout_gravity="fill"
android:layout_row="6"
android:layout_rowSpan="1"
android:layout_column="0"
android:id="@+id/friendsList"
android:layout_width="0dp"
android:layout_columnWeight="0.6"
android:layout_height="0dp"
>
</FrameLayout>
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowSpan="7"
android:layout_column="1"
android:layout_width="0dp"
android:layout_columnWeight="1.6"
android:layout_height="0dp"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</GridLayout>
我有 Activity
文件,其 onCreare
包含此代码 AFTER setContentView(R.layout.activity_event);
PlaceAutocompleteFragment autocompleteFragment =
(PlaceAutocompleteFragment) getFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i("PLACE", place.getName().toString());
}
@Override
public void onError(Status status) {
Log.i("PLACE", "ERROR WHEN AUTO COMPLETE PLACE");
}
});
问题是,setOnPlaceSelectedListener
一直给我 NullPointerException
,并说我正在尝试将侦听器设置为 null
对象。我需要在片段中使用 Google 个位置。
请注意,对于较小的屏幕,Activity
版本可以正常启动。问题仅在于此。
您需要创建 class 来扩展片段,重写 onCreateView 方法并在该方法中扩展您的片段布局 (place_autocomplete_fragment)。然后创建该 class 的对象,然后才能设置侦听器。
class FragmentClass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.place_autocomplete_fragment, container, false);
return view;
}
}
然后创建 FragmentClass 对象并设置所需的侦听器。