簡體   English   中英

PlaceAutoCompleteFragment無法啟動活動java.lang.NullPointerException

[英]PlaceAutoCompleteFragment unable to start activity java.lang.NullPointerException

每當我嘗試運行我的應用程序時,它就會崩潰或停止運行。

是否有任何解決方案或替代選擇? 我正在嘗試在我的活動中實現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

更改

froms = (PlaceAutocompleteFragment)getFragmentManager().findFragment‌​ById(R.id.froms); 

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

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

我遇到了同樣的問題,它的發生是由於將getSupportFragmentManagerPlaceAutocompleteFragment一起使用

我要做的就是使用支持人員提供的所有組件。
我的布局有
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?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM