2015-06-21 121 views
0

創建佈局我有這樣我如何添加現有的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> 

一個XML現在我想將它添加到我的主要佈局,這是我創建的代碼的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); 

但我總是得到

空指針錯誤。

我認爲這個問題是由findviewByID造成的,因爲在說明它說:「認定是由來自XML這是在的onCreate處理id屬性標識的觀點」,所以它永遠是零,但我不知道如何糾正它。

回答

0

錯誤是因爲您在致電setContentView()之前致電findViewById()。因此沒有視圖,它會返回null。您也可以致電.setId()

爲什麼不只是<@include/>佈局在另一個裏面?和setContentView的主佈局?

+0

因爲這是我公司要求的。他們已經通過CODE創建了一個Framelayout,並且他們要求我將該現有的xml添加到該佈局,所以我不能使用<@include/> – Jerrybb

相關問題