当我将片段添加到xml时,我的应用程序崩溃了

时间:2013-11-05 13:58:15

标签: android android-layout android-fragments fragment android-xml

这是我使用的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="wrap_content"
android:orientation="vertical" >

<fragment
        android:id="@+id/map"
        android:layout_width="251dp"
        android:layout_height="174dp"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        class="com.google.android.gms.maps.MapFragment" />

   <ListView
      android:id="@+id/tips"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_marginBottom="20dp"
      android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"
      android:layout_weight="1.64"
      android:clickable="false"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:listSelector="@android:color/transparent" >
   </ListView>
 </LinearLayout>

如果我不将片段添加到xml中,它工作正常但是当我将片段添加到布局时(上面添加)不起作用...问题是什么,请帮助我。

这里是清单文件

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

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />
<permission
    android:name="example.googleapi.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"></permission>
<uses-permission
    android:name="example.googleapi.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>


<application
android:permission="android.permission.WRITE_EXTERNAL_STORAGE"

android:testOnly="false"
android:debuggable="true"
android:allowBackup="true"
    android:icon="@drawable/action_icon"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme">

   <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="My API KEY"/> 

     <activity
        android:name="example.googleapi.Main_Activity_List"
        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>

3 个答案:

答案 0 :(得分:0)

您需要再添加一个权限

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

在MainActivity中,您应该扩展FragmentActivity

public class MainActivity extends android.support.v4.app.FragmentActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.Your_XML);
   }
}

我希望这可以帮到你。

答案 1 :(得分:0)

在我的情况下,我声明像这样的地图

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/rote_search_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </com.google.android.gms.maps.MapView>
...
...
...
</FrameLayout>

答案 2 :(得分:0)

// try this
**activity_main.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="wrap_content"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/maps"
        android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="350dp" />

    <ListView
        android:id="@+id/tips"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_weight="0.60"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:listSelector="@android:color/transparent"/>
</LinearLayout>

public class MainActivity extends FragmentActivity {

    GoogleMap googleMap;
    ListView tips;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tips=(ListView)findViewById(R.id.tips);
        FragmentManager fm = getSupportFragmentManager();
        SupportMapFragment f = (SupportMapFragment) fm.findFragmentById(R.id.maps);
        googleMap = f.getExtendedMap();
    }
}