Android 为什么当我将谷歌地图添加到我的应用程序时导航抽屉停止工作?
我正在开发我的第一款Android应用程序,它使用了导航抽屉和谷歌地图。我希望谷歌地图在应用程序打开时立即显示,但我也希望它在导航抽屉中选择“New Trip”时显示 导航抽屉本身工作正常。但一旦我加入谷歌地图,抽屉里的任何东西在点击时都不会打开到正确的布局。我试图为地图本身创建一个新的片段,而不是将其放在MainActivity中,但没有成功。我还学习了一些关于创建导航抽屉和将谷歌地图添加到应用程序的视频教程,但没有一个能回答我的问题: 将Google地图添加到已经有导航抽屉的应用程序时,您是否应该创建一个新的MapActivity.java和map.xml?如何使地图显示在Main活动中的Android 为什么当我将谷歌地图添加到我的应用程序时导航抽屉停止工作?,android,google-maps,android-fragments,android-studio,navigation-drawer,Android,Google Maps,Android Fragments,Android Studio,Navigation Drawer,我正在开发我的第一款Android应用程序,它使用了导航抽屉和谷歌地图。我希望谷歌地图在应用程序打开时立即显示,但我也希望它在导航抽屉中选择“New Trip”时显示 导航抽屉本身工作正常。但一旦我加入谷歌地图,抽屉里的任何东西在点击时都不会打开到正确的布局。我试图为地图本身创建一个新的片段,而不是将其放在MainActivity中,但没有成功。我还学习了一些关于创建导航抽屉和将谷歌地图添加到应用程序的视频教程,但没有一个能回答我的问题: 将Google地图添加到已经有导航抽屉的应用程序时,您是
onCreate
,以及以后在导航抽屉中进行选择时如何显示
我无法解释为什么我的导航抽屉在未更改MainActivity中的导航项Selected(MenuItem项)时停止工作:
package com.trippal.trippal;
import android.app.Dialog;
import android.app.FragmentManager;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import java.io.IOException;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;
private static final double
CSULA_LAT = 34.065207,
CSULA_LNG = -118.170125,
LASVEGAS_LAT = 36.126750,
LASVEGAS_LNG = -115.165718;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (initMap()) {
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
gotoLocation(CSULA_LAT, CSULA_LNG, 14);
} else {
Toast.makeText(this, "Map not connected!", Toast.LENGTH_SHORT).show();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_new_trip_layout) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new NewTripFragment())
.commit();
} else if (id == R.id.nav_saved_trip_layout) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new SavedTripFragment())
.commit();
} else if (id == R.id.nav_saved_poi_layout) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new SavedPOIFragment())
.commit();
} else if (id == R.id.nav_pref_layout) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new PrefFragment())
.commit();
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
// ***** map logic *****
// checks if map service is connected
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT);
}
return false;
}
// initiates google map
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
}
return (mMap != null);
}
// moves the camera location
private void gotoLocation(double lat, double lng, float zoom){
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.moveCamera(update);
}
// hides softkey
private void hideSoftKeyboard(View v){
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
// geo location
public void geoLocate(View v) throws IOException {
hideSoftKeyboard(v);
TextView tv = (TextView) findViewById(R.id.map_editText_location);
String searchString = tv.getText().toString();
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(searchString, 1);
if (list.size() > 0){
Address address = list.get(0);
String locality = address.getLocality();
Toast.makeText(this, "Found: " + locality, Toast.LENGTH_SHORT).show();
double lat = address.getLatitude();
double lng = address.getLongitude();
gotoLocation(lat, lng, 15);
}
}
}
package com.trippal.trippal;
导入android.app.Dialog;
导入android.app.FragmentManager;
导入android.location.Address;
导入android.location.Geocoder;
导入android.os.Bundle;
导入android.support.design.widget.NavigationView;
导入android.support.v4.view.GravityCompat;
导入android.support.v4.widget.DrawerLayout;
导入android.support.v7.app.ActionBarDrawerToggle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesUtil;
导入com.google.android.gms.maps.CameraUpdate;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入java.io.IOException;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity
实现NavigationView.OnNavigationItemSelectedListener{
谷歌地图;
私有静态最终整数错误对话框请求=9001;
私人静态最终双
CSULA_LAT=34.065207,
CSULA_液化天然气=-118.170125,
拉斯维加斯拉特=36.126750,
拉斯维加斯液化天然气=-115.165718;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(initMap()){
Toast.makeText(这是“准备映射!”,Toast.LENGTH_SHORT.show();
gotoLocation(CSULA_LAT,CSULA_LNG,14);
}否则{
Toast.makeText(这是“映射未连接!”,Toast.LENGTH_SHORT.show();
}
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。设置抽屉定位器(开关);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
}
@凌驾
public void onBackPressed(){
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
if(抽屉isDrawerOpen(重力压缩机启动)){
抽屉。关闭抽屉(重力压缩机启动);
}否则{
super.onBackPressed();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@SuppressWarnings(“StatementWithEmptyBody”)
@凌驾
公共布尔值onNavigationItemSelected(MenuItem项){
//处理导航视图项单击此处。
int id=item.getItemId();
FragmentManager FragmentManager=getFragmentManager();
if(id==R.id.nav\u新的\u行程\u布局){
fragmentManager.beginTransaction()
.更换(R.id.content_框架
,新的NewTripFragment())
.commit();
}else if(id==R.id.nav\u已保存\u行程\u布局){
fragmentManager.beginTransaction()
.更换(R.id.content_框架
,新的SavedTripFragment())
.commit();
}else if(id==R.id.nav\u已保存\u poi\u布局){
fragmentManager.beginTransaction()
.更换(R.id.content_框架
,新的SavedPOIFragment())
.commit();
}else if(id==R.id.nav\u pref\u布局){
fragmentManager.beginTransaction()
.更换(R.id.content_框架
,新的PrefFragment())
.commit();
}else if(id==R.id.nav_共享){
}else if(id==R.id.nav_send){
}
抽屉布局抽屉=(D
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.trippal.trippal.MainActivity"
tools:showIn="@layout/app_bar_main">
<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/map_textView_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/map_textView_location"
android:textSize="22dp" />
<EditText
android:id="@+id/map_editText_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/map_textView_location"
android:layout_toRightOf="@id/map_textView_location"
android:ems="6"
android:inputType="textCapWords"
android:textSize="22dp" />
<Button
android:id="@+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/map_textView_location"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:onClick="geoLocate"
android:text="@string/map_button_search" />
</RelativeLayout>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>