像我这样在我的项目上实施googlemap
来自xml
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp"
class="com.google.android.gms.maps.MapFragment"/>
来自java的
public class Search extends Fragment implementsOnMapReadyCallback {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
loadMap();
...
}
GoogleMap map;
MapFragment mapFragment;
public void loadMap() {
if (mapFragment == null) {
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
@Override
public void onDestroy() {
super.onDestroy();
getFragmentManager().beginTransaction().remove(mapFragment).commit();
}
即使我从一个片段更改为另一个片段,这也能正常工作。 但是,经过一段时间后,就像应用程序已经在后台运行了很长时间,当我再次尝试打开应用程序时,它只是崩溃应用程序而我收到此错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ticketing.app/com.ticketing.app.Main}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.ticketing.app.Search.onCreateView(Search.java:55)
at android.app.Fragment.performCreateView(Fragment.java:1695)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:694)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.Activity.performStart(Activity.java:5131)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f0a0065, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
at android.app.Activity.onCreateView(Activity.java:4740)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.ticketing.app.Search.onCreateView(Search.java:55)
at android.app.Fragment.performCreateView(Fragment.java:1695)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:694)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.Activity.performStart(Activity.java:5131)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
和清单文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package name is here" >
<!-- To enable the app display map, you need these permission below. without any of these permission, the app will crash.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
<activity
android:name=".Launcher"
android:label="Appname" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Create_Account"
android:label="" >
</activity>
<activity
android:name=".Sign_In"
android:label="" >
</activity>
<activity
android:name=".Main"
android:label="Search" >
</activity>
<activity
android:name=".Select_Bus"
android:label="@string/title_activity_select__bus"
android:parentActivityName=".Main" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ticketing.app.Main" />
</activity>
<!-- the metadata helps store value that will probably be different across apps but should be accessible in the same way
the ones below, help store the API key and a google play service integer. Note that these information have to be provided
when using google play service and the map API.
-->
<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 for map is here"/>
</application>
</manifest>
我该如何解决这个问题。
答案 0 :(得分:0)
我认为这个错误可以忽略,app应该加载得很好。 试试这个:
View rootView;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{
// The last two arguments ensure LayoutParams are inflated
// properly.
if (rootView != null)
{
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try
{
rootView = inflater.inflate( R.layout.yourMapFragment, container, false );
}
catch( InflateException ex )
{
}
...
return rootView;
}