我正在使用谷歌导航抽屉为我的应用程序,我正在尝试添加谷歌地图。
到目前为止,我的代码构建正常,但是当选择菜单中的地图选项(打开新片段)时,它会崩溃。
我似乎无法找到关于如何做到这一点的任何直接教程,所以任何帮助都会很棒:)
已更新
mapFrag类
public class mapFrag extends Fragment {
MapView mMapView;
GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_map, container, false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();//needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity());
googleMap = mMapView.getMap();
googleMap.moveCamera( CameraUpdateFactory. newLatLngZoom(new LatLng(**,**) , 14.0f) );
googleMap.setBuildingsEnabled(isVisible());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
//Perform any camera updates here
;
return v;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
.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"
android:screenOrientation="portrait"
>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="140dp"
>
</com.google.android.gms.maps.MapView>
</RelativeLayout>
MainActivity方法
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
SupportMapFragment mapFrag = null;
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
mapFrag = new fragName();
break;
case 5:
case 6:
default:
break;
}
if (mapFrag != null){
FragmentManager mapFragManager = getFragmentManager();
mapFragManager.beginTransaction().replace(R.id.frame_container,fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
else{
Log.e(null, "Error in creating fragment");
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
清单xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.slidingmenu"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="info.androidhive.slidingmenu.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="info.androidhive.slidingmenu.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="info.androidhive.slidingmenu.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="info.androidhive.slidingmenu.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Goolge Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="**API KEY**" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
logcat的
12-02 12:16:51.910: W/ImageView(4863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-02 12:16:51.910: W/ImageView(4863): at dalvik.system.NativeStart.main(Native Method)
12-02 12:16:58.540: D/AndroidRuntime(4863): Shutting down VM
12-02 12:16:58.540: W/dalvikvm(4863): threadid=1: thread exiting with uncaught exception (group=0x40a361f8)
12-02 12:16:58.550: E/AndroidRuntime(4863): FATAL EXCEPTION: main
12-02 12:16:58.550: E/AndroidRuntime(4863): java.lang.NullPointerException
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.app.BackStackRecord.doAddOp(BackStackRecord.java:347)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.app.BackStackRecord.replace(BackStackRecord.java:382)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.app.BackStackRecord.replace(BackStackRecord.java:374)
12-02 12:16:58.550: E/AndroidRuntime(4863): at info.androidhive.slidingmenu.MainActivity.displayView(MainActivity.java:206)
12-02 12:16:58.550: E/AndroidRuntime(4863): at info.androidhive.slidingmenu.MainActivity.access$0(MainActivity.java:172)
12-02 12:16:58.550: E/AndroidRuntime(4863): at info.androidhive.slidingmenu.MainActivity$SlideMenuClickListener.onItemClick(MainActivity.java:133)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.widget.AbsListView$1.run(AbsListView.java:3168)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.os.Handler.handleCallback(Handler.java:605)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.os.Looper.loop(Looper.java:137)
12-02 12:16:58.550: E/AndroidRuntime(4863): at android.app.ActivityThread.main(ActivityThread.java:4446)
12-02 12:16:58.550: E/AndroidRuntime(4863): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 12:16:58.550: E/AndroidRuntime(4863): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 12:16:58.550: E/AndroidRuntime(4863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-02 12:16:58.550: E/AndroidRuntime(4863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-02 12:16:58.550: E/AndroidRuntime(4863): at dalvik.system.NativeStart.main(Native Method)
12-02 12:19:19.560: I/Process(4863): Sending signal. PID: 4863 SIG: 9
我在运行android 4.0.4的物理设备上运行它
答案 0 :(得分:2)
创建一个包含地图的片段:
public class MapFragment extends Fragment
mapview.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="12"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />
然后在片段中执行此操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.mapview, container, false);
}
然后获取地图实例:
private GoogleMap getGoogleMap() {
if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) {
SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview);
if (smf != null) {
map = smf.getMap();
}
}
return map;
}