【问题标题】:Attempting to add xml dynamically in android with inflater尝试使用inflater在android中动态添加xml
【发布时间】:2013-06-23 19:11:47
【问题描述】:

我有我的主页 xml,如果用户的数据库中没有任何东西,我想将按钮加载到该页面中。

我的主页xml是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/beerTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <ImageView android:id="@+id/image"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_margin="10dip"/>



    <TableLayout  

    android:id="@+id/tableLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:layout_gravity="center_horizontal"
    android:shrinkColumns="*"  
    android:stretchColumns="*"> 

    <TableRow
        android:id="@+id/tableStatTitles"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:padding="5dip" >

        <TextView
            android:id="@+id/abvTitle"
            android:text="ABV"
            android:gravity="center" 
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_weight="1"

            ></TextView>

        <TextView
            android:id="@+id/IBUTitle"
            android:text="IBU"
            android:gravity="center" 
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_weight="1"

            ></TextView>

        <TextView
            android:id="@+id/glassTitle"
            android:text="Glass"
            android:gravity="center" 
            android:textStyle = "bold"
            android:textSize="20sp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            ></TextView>


        </TableRow>

    <TableRow
        android:id="@+id/tableStat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:padding="5dip" >

        <TextView
            android:id="@+id/abv"
            android:text=""
            android:gravity="center" 
            android:textSize="15sp"
            android:layout_width="wrap_content"
            ></TextView>

        <TextView
            android:id="@+id/IBU"
            android:text=""
            android:gravity="center" 
            android:textSize="15sp"
            android:layout_width="wrap_content"
            ></TextView>

        <TextView
            android:id="@+id/glass"
            android:text=""
            android:gravity="center" 
            android:textSize="15sp"
            android:layout_width="wrap_content"
            ></TextView>


        </TableRow>

    </TableLayout> 







    <Button
        android:id="@+id/buttonBrewery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text=""
        android:onClick="viewBrewery"
        />



    <Button
        android:id="@+id/buttonStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text=""
        android:onClick="viewStyle"

        />



    <TextView
        android:id="@+id/beerDescriptionTitle"
        android:textStyle = "bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:text="Description:"
        android:padding="5dip"
        ></TextView>
    <TextView
        android:id="@+id/beerDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="15sp"
        android:padding="5dip"

        ></TextView>

    <LinearLayout 
    android:id="@+id/addBeerLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

        </LinearLayout>

</LinearLayout>
</ScrollView>

我要添加的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="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add Beer" />

</LinearLayout>

我正在尝试将一个 xml 文件扩展为主要文件:

//inflate with add beer button
                Inflater inflater;
                LinearLayout addButton = (LinearLayout)findViewById(R.id.addBeerLayout);
                addButton.addView(inflater.inflate(R.layout.addbeerbutton_layout, null));

我的 findviewbyid 在 eclipse 中给出了这个错误:

    The method findViewById(int) is undefined for the type CheckBeerJSON
and my inflate give this error in eclipse:
The method inflate(byte[]) in the type Inflater is not applicable for the arguments (int, null)

【问题讨论】:

  • 它是否会因 NullPointerException 而崩溃?
  • 我在主帖中添加了两个错误
  • 第一个错误你需要一个上下文,第二个正确的类是 LayoutInflater

标签: android android-xml android-inflate


【解决方案1】:

组成您的 cmets CheckBeerJSON 是 Aynstask。所以你需要黑带建议的上下文。将活动上下文传递给异步任务的构造函数。

来自您的活动课程

     new CheckBeerJSON(ActivityName.this).execute(params);

然后在asynctask的构造函数中。

     Context mContext;
     public CheckBeerJSON(Context context)
     {
       mContext = context;
     } 

要插入布局,您需要LayoutInflater

   LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

还有

   LinearLayout addButton = (LinearLayout)((Activity) mContext).findViewById(R.id.addBeerLayout);
   addButton.addView(mInflater.inflate(R.layout.addbeerbutton_layout, null));  

同时确保你没有更新doInbackground中的ui。

【讨论】:

  • 非常感谢它的工作!我应该能够从这里弄清楚其余的!
【解决方案2】:

我这里没有电脑,所以我不能发布示例,但也许一个提示就足够了。

viewstub 可能是一个不错的选择,如果我正确理解您想要实现的目标。

http://developer.android.com/reference/android/view/ViewStub.html

【讨论】:

    【解决方案3】:

    我在我的应用程序中执行此操作的方式如下。

    LayoutInflater mLayoutInflater = (LayoutInflater) 
        getSystemService(LAYOUT_INFLATER_SERVICE);
    
    //Layout defined in XML
    LinearLayout mLinearLayout = (LinearLayout)
        findViewById(R.id.layout);
    
    //View to add to XML layout
    View mView = mLayoutInflater.inflate(R.layout.view,
        mLinearLayout, false);
    
    mLinearLayout.addView(mView);
    

    【讨论】:

    • layout_inflater_service 给我这个错误:LAYOUT_INFLATER_SERVICE 无法解析为变量
    • 这很奇怪,我的 Eclipse 屏幕中现在有这个确切的代码,我没有看到任何错误。也许试试(Context.LAYOUT_INFLATER_SERVICE)。您的错误可能在其他地方,您可以将其余代码发布在使用它的地方吗?
    • @Mike 是否是您的checkBeerJSON 扩展活动。
    • 它扩展了 AsyncTask,因为它调用外部 api 来查看用户的投资组合中是否有啤酒。如果他们不这样做,那么应该会出现这个按钮。
    • LAYOUT_INFLATER_SERVICE 是 Context 的成员 ...尝试使用 Context.LAYOUT_INFLATER_SERVICE,但您可能需要它的实际实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多