如何将现有xml布局添加到由代码创建的布局中

时间:2015-06-21 16:53:19

标签: android xml layout

我有像这样的xml

<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/SlidingPanel"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left">

        <ListView
            android:id="@+id/MenuList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#101010"
        android:orientation="vertical" >


        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button"
             />
    </LinearLayout>

</android.support.v4.widget.SlidingPaneLayout>

现在我想将它添加到我的主布局中,这是我通过代码创建的framelayout。这是我做的:

SlidingPaneLayout mSlidingPanel;
        Button bt;
        View slide_menu;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            slide_menu = mInflater.inflate(R.layout.activity_main, null);

//CREATE FRAMELAYOUT HERE
            FrameLayout fr = new FrameLayout(this);
            fr.setId(1);        
            ViewGroup scene1 = (ViewGroup) findViewById(fr.getId());
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

            scene1.addView(slide_menu, lp);
            //////////////
        mSlidingPanel = (SlidingPaneLayout) findViewById(R.id.SlidingPanel);
        mMenuList = (ListView) findViewById(R.id.MenuList);
        appImage = (ImageView)findViewById(android.R.id.home);
        TitleText = (TextView)findViewById(android.R.id.title);
        bt = (Button)findViewById(R.id.button1);

        for(int i = 0;i<imgID.length;i++){
            CreateObject ob = new CreateObject(imgID[i], titleContext[i], null );
            mObject.add(ob);
        }
        mListViewAdapter = new CreateListViewAdapter(this, mObject);
        mMenuList.setAdapter(mListViewAdapter);

        mSlidingPanel.setPanelSlideListener(panelListener);
        mSlidingPanel.setParallaxDistance(200);

/*
        mListViewAdapter.notifyDataSetChanged();
*/
        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(mSlidingPanel.isOpen()){
                    mSlidingPanel.closePane();
                }
                else{
                    mSlidingPanel.openPane();
                }
            }
        });

// SET CONTENTVIEW HERE
             setContentView(scene1,lp);

但我总是得到

  

NullPointer错误。

我认为问题是由findviewByID引起的,因为在描述中它说:“查找由onCreate中处理的XML中的id属性标识的视图”,所以它总是为null,但我不知道我不知道如何纠正它。

1 个答案:

答案 0 :(得分:0)

错误是因为您在致电findViewById()之前致电setContentView()。因此没有视图,它将返回null。

此外,拨打.setId()很少有好主意。

为什么不只是<@include/>布局在另一个内?和setContentView的主要布局是什么?