PlaceAutoCompleteFragment无法启动活动java.lang.NullPointerException

时间:2016-12-22 18:06:05

标签: java android autocomplete

每当我尝试运行我的应用时,它就会崩溃或停止。

是否有任何解决方案或替代方案? 我试图在我的活动中实施PlaceAutocompleteFragment

我的代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;

public class Home extends FragmentActivity {
    PlaceAutocompleteFragment autocompleteFragment,froms,tos;

    protected static final int RESULT_CODE = 123;
    private AutoCompleteTextView from;
    private AutoCompleteTextView to;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.directions_input);

        //to = (EditText) findViewById(R.id.to);
        Button btnLoadDirections = (Button) findViewById(R.id.load_directions);


        btnLoadDirections.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent data = new Intent(Home.this, DirectionsApi.class);
                data.putExtra("from", froms.getText(0).toString());
                data.putExtra("to", tos.getText(0).toString());
                startActivity(data);
            }
        });



       froms = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.froms);
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ESTABLISHMENT)
                .build();
        autocompleteFragment.setFilter(typeFilter);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
             froms.setText((CharSequence) place);
            }

            @Override
            public void onError(Status status) {

            }
        });


        tos = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.tos);
       typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ESTABLISHMENT)
                .build();
        autocompleteFragment.setFilter(typeFilter);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                tos.setText((CharSequence) place);
            }

            @Override
            public void onError(Status status) {

            }
        });
}

我的XML:

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center"
    android:padding="16dp"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/from_label"
        android:layout_gravity="fill_horizontal"
        android:text="@string/from"
        android:textColor="?android:textColorSecondary"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

 <!--   <AutoCompleteTextView
        android:id="@+id/from"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"/>-->
    <fragment
        android:id="@+id/froms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#87E886"

        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
       />

    <TextView
        android:id="@+id/to_label"
        android:layout_gravity="fill_horizontal"
        android:text="@string/to"
        android:textColor="?android:textColorSecondary"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

<!--    <AutoCompleteTextView
        android:id="@+id/to"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal" />-->
    <fragment
        android:id="@+id/tos"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#87E886"

        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

    <Button
        android:id="@+id/load_directions"
        android:layout_gravity="fill_horizontal"
        android:text="@string/load_directions"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

Logcat错误:

at com.test.test.onCreate(Home.java:81)
FATAL EXCEPTION: main
    Process: com.test.test, PID: 21545


    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.Home}: 
java.lang.NullPointerException

2 个答案:

答案 0 :(得分:0)

更改

froms = (PlaceAutocompleteFragment)getSupportFragmentManager().findF‌​ragmentById(R.id.fro‌​ms); 

<p class='date'><%# IfEmpty(Eval("DocumentTags"),"",Eval("DocumentTags").ToString() + " | ") %><%# FormatDateTime(Eval("Date"),"MMM d, yyyy") %></p>

因为您使用的是android.support.v4.app.FragmentActivity;

答案 1 :(得分:0)

我遇到了同样的问题,它是由于将 getSupportFragmentManager PlaceAutocompleteFragment 结合使用而发生的。

我要做的就是使用支持人员提供的所有组件。
我的布局有
com.google.android.gms.maps.SupportMapFragment
com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<fragment
    android:id="@+id/place_autocomplete_fragment"
    android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true" />

我的Java文件有
supportFragmentManager
SupportMapFragment
SupportPlaceAutocompleteFragment

val mapFragment = supportFragmentManager.findFragmentById(R.id.map) 
                   as SupportMapFragment?

val autocompleteFragment = supportFragmentManager
            .findFragmentById(R.id.place_autocomplete_fragment) 
                 as SupportPlaceAutocompleteFragment?