Каждый раз, когда я пытаюсь запустить свое приложение, он сбой или остановка.PlaceAutoCompleteFragment не может начать работу 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
Вы прошли через код, чтобы увидеть, какая переменная равна нулю? –
Да, Эта часть: typeFilter = new AutocompleteFilter.Builder() .setTypeFilter (AutocompleteFilter.TYPE_FILTER_ESTABLISHMENT) .build(); Похоже на причину ошибки. –