我正在开发一个应用程序,该应用程序的顶部有一个菜单,并且菜单内有一些按钮。我正在尝试将Google Maps添加到按钮之一,我尝试了以下代码,但没有显示地图。我也尝试调试我的代码,但找不到任何问题。
这是主要活动:
private DrawerLayout dr;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main2);
//drawerLayout
dr=(DrawerLayout)findViewById(R.id.draw);
toggle=new ActionBarDrawerToggle(this,dr,R.string.Open,R.string.Close);
dr.addDrawerListener(toggle);
NavigationView navigationView =(NavigationView)findViewById(R.id.navigate);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
SetUpDrawer(navigationView);
}
菜单:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(toggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
public void selectDrawer(MenuItem menu)
{
Fragment myfragmenet =null;
Class FragmentClass = null;
switch (menu.getItemId())
{
case R.id.VoirHotel:
FragmentClass=Hotels.class;
break;
case R.id.filter:
FragmentClass=Filter.class;
break;
case R.id.carte:
FragmentClass=Maps.class;
}
try {
myfragmenet=(Fragment)FragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager=getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fcontent,myfragmenet).commit();
menu.setChecked(true);
setTitle(menu.getTitle());
dr.closeDrawers();
}
private void SetUpDrawer(NavigationView navigater)
{
navigater.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
selectDrawer(menuItem);
return true;
}
});
}
}
地图片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView=inflater.inflate(R.layout.fragment_maps, container, false);
return mView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment mapFragment=(MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(new LatLng(36.7331,3.1567)).title("Hotel Hilton").snippet("Babez"));
CameraPosition lib = CameraPosition.builder().target(new LatLng(36.7194,3.1405)).zoom(16).bearing(0).tilt(45).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(lib));
}
}
这是调试控制台显示的内容:
java.lang.NullPointerException:尝试调用虚拟方法 空对象上的java.lang.Class java.lang.Object.getClass() android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:396)上的参考 在android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:444) 在android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:434) 在com.example.user.coursroyalschool.Main2Activity.selectDrawer(Main2Activity.java:81) 在com.example.user.coursroyalschool.Main2Activity $ 1.onNavigationItemSelected(Main2Activity.java:91) 在android.support.design.widget.NavigationView $ 1.onMenuItemSelected(NavigationView.java:170) 在android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
任何人都可以告诉我代码的问题在哪里吗?