我有片段导航问题。我使用GoogleMap使用片段制作了一个MainActivity。使用NavigationDrawer,我选择转到GoogleMapFragment,它可以工作,但如果我更改了片段,并想要回到我已启动的GoogleMapFragment,它就会崩溃。
LogCat:
04-24 18:03:19.576: D/OpenGLRenderer(1165): Enabling debug mode 0
04-24 18:03:23.354: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.364: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.374: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.384: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.384: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.404: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.415: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.415: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.424: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.454: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.454: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.465: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.474: W/GooglePlayServicesUtil(1165): Google Play services is missing.
04-24 18:03:23.484: W/System.err(1165): java.lang.NullPointerException
04-24 18:03:23.484: W/System.err(1165): at app.findme.map.MainActivity$GoogleMapFragment.initilizeMap(MainActivity.java:261)
04-24 18:03:23.484: W/System.err(1165): at app.findme.map.MainActivity$GoogleMapFragment.onCreateView(MainActivity.java:244)
04-24 18:03:23.494: W/System.err(1165): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
04-24 18:03:23.494: W/System.err(1165): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
04-24 18:03:23.494: W/System.err(1165): at android.app.BackStackRecord.run(BackStackRecord.java:622)
04-24 18:03:23.494: W/System.err(1165): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
04-24 18:03:23.494: W/System.err(1165): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
04-24 18:03:23.504: W/System.err(1165): at android.os.Handler.handleCallback(Handler.java:605)
04-24 18:03:23.504: W/System.err(1165): at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 18:03:23.504: W/System.err(1165): at android.os.Looper.loop(Looper.java:137)
04-24 18:03:23.504: W/System.err(1165): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 18:03:23.514: W/System.err(1165): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 18:03:23.514: W/System.err(1165): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 18:03:23.514: W/System.err(1165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 18:03:23.524: W/System.err(1165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 18:03:23.524: W/System.err(1165): at dalvik.system.NativeStart.main(Native Method)
04-24 18:03:27.805: D/dalvikvm(1165): GC_CONCURRENT freed 186K, 3% free 9287K/9543K, paused 8ms+6ms
04-24 18:03:29.425: D/AndroidRuntime(1165): Shutting down VM
04-24 18:03:29.425: W/dalvikvm(1165): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-24 18:03:29.455: E/AndroidRuntime(1165): FATAL EXCEPTION: main
04-24 18:03:29.455: E/AndroidRuntime(1165): android.view.InflateException: Binary XML file line #11: Error inflating class fragment
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-24 18:03:29.455: E/AndroidRuntime(1165): at app.findme.map.MainActivity$GoogleMapFragment.onCreateView(MainActivity.java:240)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.app.BackStackRecord.run(BackStackRecord.java:622)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.os.Handler.handleCallback(Handler.java:605)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.os.Looper.loop(Looper.java:137)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 18:03:29.455: E/AndroidRuntime(1165): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 18:03:29.455: E/AndroidRuntime(1165): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 18:03:29.455: E/AndroidRuntime(1165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 18:03:29.455: E/AndroidRuntime(1165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 18:03:29.455: E/AndroidRuntime(1165): at dalvik.system.NativeStart.main(Native Method)
04-24 18:03:29.455: E/AndroidRuntime(1165): Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Duplicate id 0x7f050008, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.app.Activity.onCreateView(Activity.java:4248)
04-24 18:03:29.455: E/AndroidRuntime(1165): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
04-24 18:03:29.455: E/AndroidRuntime(1165): ... 18 more
04-24 18:03:30.165: I/dalvikvm(1165): threadid=3: reacting to signal 3
04-24 18:03:30.185: I/dalvikvm(1165): Wrote stack traces to '/data/anr/traces.txt'
04-24 18:05:47.835: I/dalvikvm(1212): threadid=3: reacting to signal 3
04-24 18:05:47.916: I/dalvikvm(1212): Wrote stack traces to '/data/anr/traces.txt'
04-24 18:05:48.085: D/libEGL(1212): loaded /system/lib/egl/libGLES_android.so
04-24 18:05:48.105: D/libEGL(1212): loaded /system/lib/egl/libEGL_emulation.so
04-24 18:05:48.105: D/(1212): HostConnection::get() New Host Connection established 0x1d2f90, tid 1212
04-24 18:05:48.155: D/libEGL(1212): loaded /system/lib/egl/libGLESv1_CM_emulation.so
04-24 18:05:48.155: D/libEGL(1212): loaded /system/lib/egl/libGLESv2_emulation.so
04-24 18:05:48.225: W/EGL_emulation(1212): eglSurfaceAttrib not implemented
04-24 18:05:48.235: D/OpenGLRenderer(1212): Enabling debug mode 0
04-24 18:05:48.335: I/dalvikvm(1212): threadid=3: reacting to signal 3
04-24 18:05:48.356: I/dalvikvm(1212): Wrote stack traces to '/data/anr/traces.txt'
MainActivity:
package app.track.map;
import java.util.Locale;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
// Google Map
private GoogleMap googleMap;
private static String SelectedMenuPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.drawer_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
SelectedMenuPosition = getResources().getStringArray(R.array.drawer_array)[position];
// update the main content by replacing fragments
if ( position == 2){
Fragment fragment = new GoogleMapFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
else if( position == 3){
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
else
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(SelectedMenuPosition.equals("Navigate")){
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
return true;
}
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
public static class GoogleMapFragment extends Fragment {
// Google Map
private GoogleMap googleMap;
public GoogleMapFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
getActivity().setTitle(SelectedMenuPosition);
return rootView;
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setMyLocationEnabled(true);
LatLng sydney = new LatLng(-33.867, 151.206);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
googleMap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
// check if map is created successfully or not
if (googleMap == null) {
//Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
}
}
Googlemap视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
提前致谢,
最好的问候。
答案 0 :(得分:2)
你应该覆盖这个方法:(在你点击另一个片段后它会正确地破坏google_map_fragment。)
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
如果你想每次初始化地图片段,你必须覆盖这个方法:)
@Override
public void onPause(){
super.onPause();
if(googleMap != null) {
googleMap = null;
{
}