【问题标题】:My app crashes when I add fragment to xml当我将片段添加到 xml 时,我的应用程序崩溃了
【发布时间】:2013-11-05 13:58:14
【问题描述】:

这是我使用的 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>

【问题讨论】:

  • 发布 logact 和你的清单文件
  • log cat error:threadid=1: thread exiting with uncaught exception (group=0x411ad438) 我已将所有必需的信息添加到清单中以使用 google map API v2...
  • 也许是关于许可(与地图有关?)。你添加了互联网权限吗?
  • 是的,我已经添加了所有必需的权限
  • @NaveedAli 你能发布堆栈跟踪吗

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


【解决方案1】:

就我而言,我这样声明地图

    <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】:
    // 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();
        }
    }
    

    【讨论】:

      【解决方案3】:

      您需要再添加一项权限

      <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);
         }
      }
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-23
        • 1970-01-01
        相关资源
        最近更新 更多