java - 获取带有 fragment 的布局的 inflate ViewGroup

标签 java android eclipse android-fragments

我实际上正在编写一个使用 Google Maps API 的 Android 应用程序。

布局activity_main.xml如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/mapContainer"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".MainActivity" >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          map:mapType="normal"
          map:uiCompass="false"
          map:uiRotateGestures="true"
          map:uiScrollGestures="true"
          map:uiTiltGestures="true"
          map:uiZoomControls="false"
          map:uiZoomGestures="true" />
</LinearLayout>

我在 fragment 中异步加载 map 。当我尝试膨胀 Activity_main View 时:

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup mainView = (ViewGroup) inflater.inflate(R.layout.activity_main, null);

应用程序崩溃并返回错误:

01-15 19:25:16.045: E/AndroidRuntime(21115): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment

如何解决?我需要这个 ViewGroup 来为应用程序设置一个电源按钮 KeyListener(类似于我在另一个没有 fragment 的应用程序中所做的操作):

mainView.setOnKeyListener(new OnKeyListener() 
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) 
        {
            if(keyCode == KeyEvent.KEYCODE_POWER)
            {
                //STUFF
            }
        }
    });

谢谢你的帮助:D

最佳答案

命名空间必须在第一个 View 中声明

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          android:id="@+id/mapContainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity" >

<fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      map:mapType="normal"
      map:uiCompass="false"
      map:uiRotateGestures="true"
      map:uiScrollGestures="true"
      map:uiTiltGestures="true"
      map:uiZoomControls="false"
      map:uiZoomGestures="true" />
</LinearLayout>

关于java - 获取带有 fragment 的布局的 inflate ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970506/

相关文章:

java - 奇怪的 Javax.measure.quantity.Length 绑定(bind)不匹配

java - Gridgain:java.lang.OutOfMemoryError:超出 GC 开销限制

java - 映射主键由两个外键组成? hibernate

android - MotionLayout 是否支持 fragment 之间的共享元素转换?

android - fragment getArguments() 返回 null

java - 如何诊断 Java 中孤立线程的来源?

java - 创建一个类变量只是为了在Optional.ifPresentOrElse中使用是应该避免的做法吗?

java - 我的比较器没有正确排序这个 PriorityQueue?

java - Java Client API 用于访问 Hbase 的用户

android - 在 Android MediaStore.EXTRA_DURATION_LIMIT 中无法在 6.0 及更高版本的设备中工作