2017-11-15 51 views
0

次のXMLは、fragmentが追加されたことを示しています。 これはかなりですが、下部にはfragmentがあります。他のUI要素は無関係です、私は完全性のためにそれらを含めました。PlaceAutoCompleteFragment findFragmentById経由でアクセスするとNullPointerExceptionが発生する

<?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> 

そして私は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"); 
    } 
}); 

Activityファイル、問題がある、setOnPlaceSelectedListenerは私にNullPointerExceptionすべての時間を与え、私は言うを持っていますリスナーをnullオブジェクトに設定しようとしています。 これを整理して断片化してGoogleプレイスを使用するには、助けが必要です。

小さい画面では、Activityのバージョンが正常に開始されます。問題はこの問題だけです。

答えて

0

フラグメントを拡張し、onCreateViewメソッドをオーバーライドし、そのメソッドでフラグメントレイアウト(place_autocomplete_fragment)を拡張するクラスを作成する必要があります。その後、そのクラスのオブジェクトを作成し、それからあなたはリスナーを設定することができます。

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オブジェクトを作成し、目的のリスナーを設定します。

関連する問題