[英]How can I add an existing xml layout to a layout that created by code
I have an xml like this 我有一个这样的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>
Now I want to add it to my main layout, which is a framelayout I created by code. 现在,我想将其添加到我的主布局中,这是我通过代码创建的框架布局。 Here is what I did:
这是我所做的:
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);
But I always get 但是我总是
NullPointer error.
NullPointer错误。
I think the problem is caused by findviewByID, because in description it says that: "Finds a view that was identified by the id attribute from the XML that was processed in onCreate", so it will always be null, but I don't know how to correct it. 我认为问题是由findviewByID引起的,因为在描述中它说:“从onCreate中处理的XML中找到由id属性标识的视图”,因此它始终为null,但我不知道如何纠正它。
The error is because you are calling findViewById()
before you call setContentView()
. 该错误是因为您在调用
setContentView()
之前先调用findViewById()
setContentView()
。 Therefore there is no view, and it will return null. 因此,没有视图,它将返回null。
Also, it's rarely a good idea to call .setId()
. 另外,调用
.setId()
是一个好主意。
Why not just <@include/>
the layout inside another? 为什么不只是在另一个内部布局
<@include/>
? And setContentView the main layout? 和setContentView的主要布局?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.