简体   繁体   English

getsupportfragmentmanager().findfragmentbyid(r.id.map) 返回 null 谷歌 map

[英]getsupportfragmentmanager().findfragmentbyid(r.id.map) returns null Google map

I try to use google map functionality on my MainActivity I have this null pointer returned by: getsupportfragmentmanager().findfragmentbyid(r.id.map);我尝试在我的 MainActivity 上使用谷歌 map 功能我有这个 null 指针返回:getsupportfragmentmanager().findfragmentbyid(r.id.map); ==> return null ==> 返回 null

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapApi);

mapfragment will be null and when I call it using getMapAsync(this); mapfragment 将是 null 并且当我使用 getMapAsync(this) 调用它时; the app shut down before it starts应用程序在启动前关闭

 mapFragment.getMapAsync(this); // null pointer variable call getMapAsync 

content_main.xml content_main.xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="216dp"
        android:orientation="horizontal">

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

        </fragment>
    </LinearLayout>

onCreate inside the MainActivity: MainActivity 中的 onCreate:

public class MainActivity extends AppCompatActivity
        implements  NavigationView.OnNavigationItemSelectedListener , OnMapReadyCallback {



    private GoogleMap map;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   //setContentView(R.layout.activity_main); didn't work
setContentView(R.layout.content_main); // worked, now i haven't null pointer 
        System.out.println("************************ mapApi:"+R.id.mapApi);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapApi);


        System.out.println("************************ mapFragment:"+mapFragment);

        if (mapFragment != null) {

            mapFragment.getMapAsync(this);
        }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
    LatLng BordjMenaielGare = new LatLng(36.753162, 3.717716);
    LatLng Blida = new LatLng(36.769038, 3.057451);
    map.addMarker(new MarkerOptions().position(BordjMenaielGare).title("Gare Bordj Menaiel "));
    map.addMarker(new MarkerOptions().position(Blida).title("Gare Agha"));
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(BordjMenaielGare, 10));
}

} }

here are the files:这里是文件:

the map fragment is on content_main.xml, I included it in app_bar_main.xml, and app_bar_main included in activity_main.xml and I'm calling the map from MainActivity the map fragment is on content_main.xml, I included it in app_bar_main.xml, and app_bar_main included in activity_main.xml and I'm calling the map from MainActivity

在此处输入图像描述

I searched a lot in StackOverflow but none of the solutions are valid for my case我在 StackOverflow 中搜索了很多,但没有一个解决方案对我的情况有效

can someone help me with this?有人可以帮我弄这个吗?

you need to call:你需要打电话:

// add this (supplying the layout for your activity)
setContentView(R.layout.activity); 

after your call to super.onCreate(savedInstanceState);在你打电话给super.onCreate(savedInstanceState);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM