`getSupportFragmentManager().findFragmentById(R.id.map)` 返回空值,Google 地图。

huangapple go评论98阅读模式
英文:

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

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;216dp&quot;
        android:orientation=&quot;horizontal&quot;&gt;

        &lt;fragment
            android:id=&quot;@+id/mapApi&quot;
            android:name=&quot;com.google.android.gms.maps.SupportMapFragment&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;&gt;

        &lt;/fragment&gt;
    &lt;/LinearLayout&gt;

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&#39;t work
setContentView(R.layout.content_main); // worked, now i haven&#39;t null pointer 
        System.out.println(&quot;************************ mapApi:&quot;+R.id.mapApi);

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


        System.out.println(&quot;************************ mapFragment:&quot;+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(&quot;Gare Bordj Menaiel &quot;));
    map.addMarker(new MarkerOptions().position(Blida).title(&quot;Gare Agha&quot;));
    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

`getSupportFragmentManager().findFragmentById(R.id.map)` 返回空值,Google 地图。

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);

huangapple
  • 本文由 发表于 2020年4月7日 01:28:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61065465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定