英文:
getsupportfragmentmanager().findfragmentbyid(r.id.map) returns null Google map
问题
我试图在我的MainActivity上使用Google地图功能,但我在此处收到空指针错误:
getsupportfragmentmanager().findfragmentbyid(r.id.map); ==> 返回 null
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapApi);
当我使用 getMapAsync(this); 调用它时,应用在启动之前就崩溃了。
在 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>
在 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.content_main);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapApi);
        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));
    }
}
你可以看到,地图片段在 content_main.xml 文件中定义,然后在 MainActivity 中使用 getSupportFragmentManager().findFragmentById(R.id.mapApi) 获取该片段,并在获取到片段后,通过 mapFragment.getMapAsync(this) 异步获取地图。
图片链接已被删除,但我可以看到你在问题描述中提到了图片的链接。如果你需要更多帮助,请告诉我。
英文:
I try to use google map functionality on my MainActivity I have this null pointer returned by :
getsupportfragmentmanager().findfragmentbyid(r.id.map); ==> return null
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapApi);
mapfragment will be null and when I call it using getMapAsync(this); the app shut down before it starts
 mapFragment.getMapAsync(this); // null pointer variable call getMapAsync 
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 :
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
I searched a lot in StackOverflow but none of the solutions are valid for my case
can someone help me with this?
答案1
得分: 1
你需要调用:
// 在调用 super.onCreate(savedInstanceState); 之后添加以下代码(为你的活动提供布局)
setContentView(R.layout.activity);
英文:
you need to call:
// add this (supplying the layout for your activity)
setContentView(R.layout.activity); 
after your call to super.onCreate(savedInstanceState);
专注分享java语言的经验与见解,让所有开发者获益!


评论