2

Загружаю GoogleMap в андроид приложение, картына девайсе отображаются, но при подключеннии <fragment> приложение падает. Googleplay and supportv4 подключил, в манифесте все норм. Также при включении setContentView(R.layout.main) приложение всегда падает и idea ошибку показывает на этой строчке. Прошу помощи, как это исправить?

MyActivity:

private GoogleMap map;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //setContentView(R.layout.main);
   SupportMapFragment fragment = new SupportMapFragment();
    getSupportFragmentManager().beginTransaction()
            .add(android.R.id.content, fragment).commit();

     map  = fragment.getMap(); //всегда null!!! как исправить?
    if(map == null) {
        Log.i("STOP", "!map");
    }

}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
                android:id="@+id/btnTest"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onClickTest"
                android:text="dsfgdfg">
        </Button>
    </LinearLayout>
    <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    </fragment>
</LinearLayout>

1 ответ 1

1

Карту получайте в onActivityCreated()

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    map = getMap();
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.