LiDaming_and 2013-01-16 09:27 采纳率: 100%
浏览 3968
已采纳

用引用类 inflate 布局失败

我想 inflate 包含类的布局。用下面的代码创建了 inflater 实例

LayoutInflater inflate =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
mRelLayout = (RelativeLayout) inflate.inflate(
                    R.layout.app_layout, null);  

app_layout.xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            xmlns:panel="http://schemas.android.com/apk/res/com.alpha"
            android:id="@+id/layoutMain"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
            android:gravity="top|right">

然而Inflation 失败,出现了异常: java.lang.RuntimeException,说我的面板必须有有一个id属性是'R.id.panelHandle'的子视图。
这是怎么回事呢?

  • 写回答

3条回答

  • hcg_21 2013-01-17 02:35
    关注

    我用过以下的代码,能实现。
    Test.java

    LayoutInflater inflate =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
         RelativeLayout mRelLayout = (RelativeLayout) inflate.inflate(
                             R.layout.app_layout, null);  
    

    app_layout.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                xmlns:panel="http://schemas.android.com/apk/res/com.alpha"
                android:id="@+id/layoutMain"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" 
                android:gravity="top|right" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备