gpt4 book ai didi

java - 谷歌地图返回空指针异常

转载 作者:行者123 更新时间:2023-12-02 03:44:33 32 4
gpt4 key购买 nike

当我尝试在 fragment 上显示 map 时,它返回空指针异常。我已将所有内容添加到 list 中的每个权限中。我正在附加 fragment 文件、其 XML 文件和日志目录

Chatffragment.java

  public class ChatFragment extends Fragment{
// Google Map
private GoogleMap googleMap;

public ChatFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{

View chatfrag = inflater.inflate(R.layout.chatfragment, container, false);


try {
// Loading map
initilizeMap();

} catch (Exception e) {
e.printStackTrace();
}
return chatfrag;
}
/**
* function to load map. If map is not created it will create it for you
* */

private void initilizeMap()
{
if (googleMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

googleMap = mapFrag.getMap();
googleMap.setMyLocationEnabled(true);

// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}

@Override
public void onResume() {
super.onResume();
initilizeMap();


}




}

它的 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

</RelativeLayout>

日志记录

04-05 09:12:22.075 26987-26987/com.example.vikrant.safeshelter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vikrant.safeshelter, PID: 26987


java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
at com.example.vikrant.safeshelter.ChatFragment.initilizeMap(ChatFragment.java:55)
at com.example.vikrant.safeshelter.ChatFragment.onResume(ChatFragment.java:70)
at android.support.v4.app.Fragment.performResume(Fragment.java:2020)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:164)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1627)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:837)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:809)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1077)
at android.support.design.widget.TabLayout$1.onClick(TabLayout.java:643)
at android.view.View.performClick(View.java:4759)
at android.view.View$PerformClick.run(View.java:19770)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5234)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

第 55 行和第 70 行是googleMap = mapFrag.getMap();initilizeMap();

最佳答案

您必须等待 map 加载......

public class ChatFragment extends Fragment implements OnMapReadyCallback {

private void initilizeMap() {
//SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);

SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}

@Override
public void onMapReady(final GoogleMap map) {
googleMap = map;
//Do other stuff..........
}

}

编辑

SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

检查编辑代码,使用ChildFragmentManager。

关于java - 谷歌地图返回空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36424991/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com