【问题标题】:PlaceAutoCompleteFragment unable to start activity java.lang.NullPointerExceptionPlaceAutoCompleteFragment 无法启动活动 java.lang.NullPointerException
【发布时间】:2018-10-03 20:04:21
【问题描述】:

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

是否有任何解决方案或替代选择? 我正在尝试在我的活动中实现 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

【问题讨论】:

  • 你有没有通过代码查看哪个变量为空?
  • 是的,这部分:typeFilter = new AutocompleteFilter.Builder() .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ESTABLISHMENT) .build();似乎是错误的原因。

标签: java android autocomplete


【解决方案1】:

改变

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

到

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

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

【讨论】:

  • 不可转换的类型;不能将 'android.support.v4.app.Fragment' 转换为 com.google.android.gms.location.PlaceAutocompleteFragment '。
【解决方案2】:

我遇到了同样的问题,这是因为将 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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2014-07-18
    • 2014-05-10
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    相关资源
    最近更新 更多