我正在尝试在我正在开发的Android应用程序上实现google map api v2,对于java类我正在扩展Fragment,因为我正在使用导航抽屉。
问题是:当您尝试从导航栏中点击它时,谷歌地图将首次运行,但是当您第二次再次点击它时它会崩溃。
这是java类:
public class FindUs_fragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout= (View)inflater.inflate(R.layout.map_activity, container,false);
return layout;
}
这是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="15"/>
</LinearLayout>
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.application.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<permission
android:name="com.application.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- The following two permissions are not required to use -->
<!-- Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.application.example.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="api key" />
</application>
</manifest>
这是logcat报告:
01-21 16:19:28.884: E/AndroidRuntime(10557): FATAL EXCEPTION: main
01-21 16:19:28.884: E/AndroidRuntime(10557): Process: com.application.example, PID: 10557
01-21 16:19:28.884: E/AndroidRuntime(10557): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-21 16:19:28.884: E/AndroidRuntime(10557): at com.application.example.FindUs_fragment.onCreateView(FindUs_fragment.java:37)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.Fragment.performCreateView(Fragment.java:1700)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.BackStackRecord.run(BackStackRecord.java:684)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.os.Handler.handleCallback(Handler.java:733)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.os.Handler.dispatchMessage(Handler.java:95)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.os.Looper.loop(Looper.java:137)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-21 16:19:28.884: E/AndroidRuntime(10557): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 16:19:28.884: E/AndroidRuntime(10557): at java.lang.reflect.Method.invoke(Method.java:515)
01-21 16:19:28.884: E/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-21 16:19:28.884: E/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-21 16:19:28.884: E/AndroidRuntime(10557): at dalvik.system.NativeStart.main(Native Method)
01-21 16:19:28.884: E/AndroidRuntime(10557): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f050015, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.app.Activity.onCreateView(Activity.java:4790)
01-21 16:19:28.884: E/AndroidRuntime(10557): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
我可以知道导致应用崩溃的问题是什么?
感谢您的时间。
更新
@Dimmerg:我将FindUs_fragment附加到导航抽屉,以下是导航抽屉活动的代码:
public class MainActivity extends Activity {
private String[] drawerListViewItems;
private ListView drawerListView;
private DrawerLayout drawerlayout;
private ActionBarDrawerToggle actionbardrawertoggle;
Fragment fragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerListViewItems = getResources().getStringArray(R.array.drawer_item);
drawerListView = (ListView)findViewById(R.id.left_drawer);
drawerListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_listview_item, drawerListViewItems));
drawerlayout = (DrawerLayout)findViewById(R.id.drawer_layout);
actionbardrawertoggle = new ActionBarDrawerToggle(
this,
drawerlayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
);
drawerlayout.setDrawerListener(actionbardrawertoggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
actionbardrawertoggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
actionbardrawertoggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(actionbardrawertoggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
@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;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
displayView(position);
//drawerlayout.closeDrawer(drawerListView);
}
}
private void displayView(int position)
{
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Home_fragment();
break;
case 1:
fragment = new FindUs_fragment();
break;
case 3:
fragment = new Facebook_fragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
drawerListView.setSelection(position);
setTitle(drawerListViewItems[position]);
drawerlayout.closeDrawer(drawerListView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
答案 0 :(得分:0)
在重新加载之前,您需要将谷歌地图设为null。 这对我有用。
@Override
public void onDestroyView() {
SupportMapFragment f = (SupportMapFragment) getFragmentManager()
.findFragmentById(id);
if (f != null) {
try {
getFragmentManager().beginTransaction().remove(f).commit();
} catch (Exception e) {
e.printStackTrace();
}
}
if (mMap != null) {
List<Marker> temp = mMap.getMarkers();
if (temp != null) {
for (Marker marker : temp) {
marker.remove();
marker = null;
}
}
}
mMap = null;
super.onDestroyView();
}