从另一个xml Android获取linearLayout

从另一个xml Android获取linearLayout,android,xml,android-linearlayout,Android,Xml,Android Linearlayout,需要获取不在主xml文件(我在setContentView()中设置的文件)中的LinearLayout。因此,请编写以下代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout main = (LinearLayout) findViewById(R.id.ma

需要获取不在主xml文件(我在setContentView()中设置的文件)中的LinearLayout。因此,请编写以下代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout main = (LinearLayout) findViewById(R.id.main1);

    LinearLayout oggetto = (LinearLayout) findViewById(R.id.element1);

    main.addView(oggetto);
}
写入element1的xml文件为:

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/element1"
            android:layout_width="fill_parent"
            android:orientation="horizontal" 
            android:layout_height="90px"
            android:background="#000000">

            <LinearLayout
                android:id="@+id/linearLayoutLeftBar"
                android:layout_width="10px"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:background="#7FFF00" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayoutRightContent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:background="#EEEEEE" >

                <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="fill_parent"
                    android:layout_height="45px"
                    android:orientation="horizontal"
                    android:background="#EEEEEE" >

                    <LinearLayout
                        android:id="@+id/linearLayoutTxtView1"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:background="#EEEEEE"
                        android:layout_gravity="right" >

                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:text="TextBox1"
                            android:layout_marginLeft="5px"
                            android:textColor="#000000"
                            android:gravity="center" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linearLayoutImgView"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:gravity="right|center" >

                        <ImageView
                            android:id="@+id/imageView1"
                            android:layout_width="16px"
                            android:layout_height="16px"
                            android:layout_marginRight="16px"
                            android:src="@drawable/ic_launcher"
                            android:background="#FF0000"/>

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="fill_parent"
                    android:layout_height="45px"
                    android:orientation="horizontal"
                    android:background="#EEEEEE" >

                    <LinearLayout
                        android:id="@+id/linearLayoutTxtView1"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:background="#EEEEEE"
                        android:layout_gravity="right" >

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:text="TextBox2"
                            android:layout_marginLeft="5px"
                            android:textColor="#000000"
                            android:gravity="center" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linearLayoutImgView"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:orientation="vertical"
                        android:gravity="right|center" >

                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:layout_marginRight="16px"
                            android:text="TextBox3"
                            android:textColor="#000000"
                            android:gravity="center|right" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>
        </LinearLayout>

问题是启动应用程序时出现错误:无法启动活动组件信息{com.chiamata/com.chiamata.ChiamataActivity}:java.lang.NullPointerException 这是因为element1为null。如果我在将其添加到main Linearlayout之前放入If(element1!=null),则一切正常,但显然没有添加element1。我需要以编程方式添加它,所以不能将所有内容都放在一个xml文件中。 我该怎么办? 谢谢你,马蒂亚你可以说

LinearLayout element1 = (LinearLayout) findViewById(R.id.element1);

main.addView(element1);

如果我正确理解你的问题,你可以使用

LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);              

LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.Your_layout_file_with_element1 , null);

然后可以使用
main.addView(布局)
添加外部布局。

为什么不能将其放在一个XML中,然后将可见性属性从GONE切换到VISIBLE?

element1在这里为空。您认为需要说LinearLayout element1=(LinearLayout)findViewById(R.id.element1);是的,很抱歉我在这里写错了,但在java中,项目名为OGGETOW,这是您的\u布局\u文件\u和\u元素1的含义