Android应用程序中的运行时异常:无法使MapFragment膨胀

时间:2014-08-11 11:43:26

标签: android

我'我发布了一个类似于这个的问题,我'已经将建议的更改添加到我 ,但我无法理解为什么还要启动此例外。

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testing"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <permission
        android:name="com.example.testing.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.example.testing.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_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.example.testing.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

mainActivity代码:

public class MainActivity extends Activity implements OnClickListener, GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private static final long UPDATE_INTERVAL = 5000;
    private static final long FASTEST_INTERVAL = 1000;

    private Button showBtn;
    private GoogleMap map = null;
    private LocationClient locationClient;
    private Location myLocation;
    private LocationRequest locationRequest;
    private Display display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showBtn = (Button) findViewById(R.id.showBtn);
        showBtn.setOnClickListener(this);
        showBtn.setClickable(false);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        display = new Display(map);

        locationClient = new LocationClient(this, this, this);
        locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(UPDATE_INTERVAL)
                .setFastestInterval(FASTEST_INTERVAL).setSmallestDisplacement(10);

        if (locationClient != null)
            locationClient.connect();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(this, "Location listener called", Toast.LENGTH_SHORT).show();
        LatLng newPosition = new LatLng(location.getLatitude(), location.getLongitude());

        myLocation = location;
        if (myLocation != null)
            showBtn.setClickable(true);
        else
            showBtn.setClickable(false);

        display.clearMap();
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(newPosition, 16));
        display.addMarker(newPosition, "My Title", "..and my comments go here");
        display.setCircleOnMap(newPosition);
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
    }

    @Override
    public void onConnected(Bundle arg0) {
        Toast.makeText(this, "Locationclient Connected", Toast.LENGTH_SHORT).show();
        locationClient.requestLocationUpdates(locationRequest, this);
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.showBtn) {
            if (InternetStatus.getInstance(this).isOnline(this)) {
                new ShowCloseUps(display, myLocation).execute();
            } else
                Toast.makeText(this, "No internet connection find one and retry!!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onDestroy() {
        if (locationClient.isConnected() && locationClient != null) {
            locationClient.removeLocationUpdates(this);
            locationClient.disconnect();
        }
        super.onDestroy();
    }
}

activity_main.xml中

    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" >

        <Button 
          android:id="@+id/showBtn"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:text="@string/show"
          />

      <fragment 
            xmlns:map="http://schemas.android.com/apk/res-auto" 
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@id/showBtn"
               android:name="com.google.android.gms.maps.MapFragment"
              map:cameraZoom="5"
              map:cameraTilt="30"
              map:uiCompass="false"
              map:uiRotateGestures="true"
              map:uiTiltGestures="true"
              map:uiZoomGestures="true"
              />
</RelativeLayout>

我的logcat标签更新:

    08-11 08:46:40.250: E/AndroidRuntime(1832): FATAL EXCEPTION: main
08-11 08:46:40.250: E/AndroidRuntime(1832): Process: com.example.testing, PID: 1832
08-11 08:46:40.250: E/AndroidRuntime(1832): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.os.Looper.loop(Looper.java:136)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at java.lang.reflect.Method.invoke(Method.java:515)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at dalvik.system.NativeStart.main(Native Method)
08-11 08:46:40.250: E/AndroidRuntime(1832): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.Activity.setContentView(Activity.java:1929)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at com.example.testing.MainActivity.onCreate(MainActivity.java:39)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.Activity.performCreate(Activity.java:5231)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-11 08:46:40.250: E/AndroidRuntime(1832):     ... 11 more
08-11 08:46:40.250: E/AndroidRuntime(1832): Caused by: java.lang.IllegalArgumentException: Binary XML file line #25: Must specify unique android:id, android:tag, or have a parent with an id for null
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.app.Activity.onCreateView(Activity.java:4759)
08-11 08:46:40.250: E/AndroidRuntime(1832):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
08-11 08:46:40.250: E/AndroidRuntime(1832):     ... 21 more

2 个答案:

答案 0 :(得分:3)

从片段中删除此行

 xmlns:map="http://schemas.android.com/apk/res-auto" 

来自activity_main.xml档案。

同样删除这一个:

map:cameraZoom="5"
map:cameraTilt="30"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiTiltGestures="true"
map:uiZoomGestures="true"

答案 1 :(得分:1)

尝试更改

<fragment 
            xmlns:map="http://schemas.android.com/apk/res-auto" 
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@id/showBtn"
               android:name="com.google.android.gms.maps.MapFragment"
              map:cameraZoom="5"
              map:cameraTilt="30"
              map:uiCompass="false"
              map:uiRotateGestures="true"
              map:uiTiltGestures="true"
              map:uiZoomGestures="true"
              />

<fragment 

              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@id/showBtn"
               android:name="com.google.android.gms.maps.MapFragment"
              map:cameraZoom="5"
              map:cameraTilt="30"
              map:uiCompass="false"
              map:uiRotateGestures="true"
              map:uiTiltGestures="true"
              map:uiZoomGestures="true"
              />

希望这可以帮到你