我有一个扩展FragmentActivty的主类和扩展了SupportMapFragment的CustomMapFragment,当我将我的customMapFragment称为FragmentActivity时,我在logCat上看到此错误。
LogCat结果:
01-05 17:43:13.916: D/ERROR(2371): Binary XML file line #2: Error inflating class fragment
01-05 17:43:13.936: W/System.err(2371): android.view.InflateException: Binary XML file line 2: Error inflating class fragment
01-05 17:43:13.936: W/System.err(2371): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-05 17:43:13.946: W/System.err(2371): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
01-05 17:43:13.958: W/System.err(2371): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-05 17:43:13.958: W/System.err(2371): at com.main.whereismyfriend.CustomMapFragment .onCreateView(CustomMapFragment .java:48)
01-05 17:43:13.958: W/System.err(2371): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-05 17:43:13.958: W/System.err(2371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-05 17:43:13.966: W/System.err(2371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-05 17:43:13.966: W/System.err(2371): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-05 17:43:13.966: W/System.err(2371): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-05 17:43:13.976: W/System.err(2371): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-05 17:43:14.006: W/System.err(2371): at android.os.Handler.handleCallback(Handler.java:730)
01-05 17:43:14.006: W/System.err(2371): at android.os.Handler.dispatchMessage(Handler.java:92)
01-05 17:43:14.016: W/System.err(2371): at android.os.Looper.loop(Looper.java:137)
01-05 17:43:14.035: W/System.err(2371): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-05 17:43:14.035: W/System.err(2371): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 17:43:14.056: W/System.err(2371): at java.lang.reflect.Method.invoke(Method.java:525)
01-05 17:43:14.066: W/System.err(2371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-05 17:43:14.075: W/System.err(2371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-05 17:43:14.075: W/System.err(2371): at dalvik.system.NativeStart.main(Native Method)
01-05 17:43:14.096: W/System.err(2371): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f050011, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
01-05 17:43:14.156: W/System.err(2371): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
01-05 17:43:14.166: W/System.err(2371): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-05 17:43:14.166: W/System.err(2371): ... 18 more
01-05 17:43:14.186: D/AndroidRuntime(2371): Shutting down VM
01-05 17:43:14.186: W/dalvikvm(2371): threadid=1: thread exiting with uncaught exception (group=0x41465700)
01-05 17:43:14.256: E/AndroidRuntime(2371): FATAL EXCEPTION: main
01-05 17:43:14.256: E/AndroidRuntime(2371): java.lang.NullPointerException
01-05 17:43:14.256: E/AndroidRuntime(2371): at com.main.whereismyfriend.CustomMapFragment .onResume(CustomMapFragment .java:96)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.support.v4.app.Fragment.performResume(Fragment.java:1521)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.os.Handler.handleCallback(Handler.java:730)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.os.Handler.dispatchMessage(Handler.java:92)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.os.Looper.loop(Looper.java:137)
01-05 17:43:14.256: E/AndroidRuntime(2371): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-05 17:43:14.256: E/AndroidRuntime(2371): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 17:43:14.256: E/AndroidRuntime(2371): at java.lang.reflect.Method.invoke(Method.java:525)
01-05 17:43:14.256: E/AndroidRuntime(2371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-05 17:43:14.256: E/AndroidRuntime(2371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-05 17:43:14.256: E/AndroidRuntime(2371): at dalvik.system.NativeStart.main(Native Method)
主要活动 :
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
CustomMapFragment fragment = new CustomMapFragment (getApplicationContext(),this);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment ).commit();
...
}
CustomMapFragment:
public class CustomMapFragment extends SupportMapFragment implements ConnectionCallbacks,OnConnectionFailedListener,LocationListener {
private LocationClient locationClient;
private LocationRequest locationRequest;
private GoogleMap map;
FragmentActivity activity;
private Context context;
View view;
public CustomMapFragment (Context context,FragmentActivity activity){
this.context = context;
this.activity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
try {
view = inflater.inflate(R.layout.map_layout, container,false);
SupportMapFragment suppFragManager = (SupportMapFragment)activity.getSupportFragmentManager().findFragmentById(R.id.map);
map = suppFragManager.getMap();
locationClient = new LocationClient(context,this,this);
locationRequest = LocationRequest.create();
locationRequest.setInterval(9000);
locationRequest.setFastestInterval(900);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return view;
} catch (Exception e) {
Log.d("ERROR", e.getMessage());
e.printStackTrace();
}
return null;
}
...
map_layout:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
AndroidManifest.xml:
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<permission
android:name="com.main.whereismyfriend.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.main.whereismyfriend.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<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="***" />
...
</application>
注意:我在此类中使用android.support.v4 lib
最佳答案
public class CustomMapFragment extends SupportMapFragment
所以
CustomMapFragment
是SupportMapFragment
...view = inflater.inflate(R.layout.map_layout, container,false);
...您试图将另一个
SupportMapFragment
充入其中。这将无法正常工作。
CustomMapFragment
扩展SupportMapFragment
(并删除其onCreateView()
加map_layout.xml
),或者使用您的map_layout.xml
。两者都不做。关于android - SupportMapFragment-二进制xml文件第2行:膨胀类 fragment 时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20940572/