Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建xml布局的对象_Android_Xml_Object_View - Fatal编程技术网

Android 创建xml布局的对象

Android 创建xml布局的对象,android,xml,object,view,Android,Xml,Object,View,我想通过创建视图对象的实例将视图添加到我的活动中,所以假设这是我的活动 public class ConverTo extends FragmentActivity private LinearLayout l1; private LengthFragment test; private static final int RESULT_SETTINGS = 1; @Override public void onCreate(Bundle savedInstanceState) { //me

我想通过创建视图对象的实例将视图添加到我的活动中,所以假设这是我的活动

public class ConverTo extends FragmentActivity

private LinearLayout l1;
private LengthFragment test;

private static final int RESULT_SETTINGS = 1;

@Override
public void onCreate(Bundle savedInstanceState) { //methods to call on app launch 
    super.onCreate(savedInstanceState);

    setContentView(R.layout.converto_home);  


    pagerAdapter = new MainPagerAdapter();
    pager = (ViewPager) findViewById (R.id.pager);
    pager.setAdapter (pagerAdapter);
    test = new LengthFragment(ConverTo.this);

     l1 = (LinearLayout) getLayoutInflater().inflate(R.layout.view_temperature, null);
    addView(l1);
执行此操作时^^会正确显示视图。而不是这样做^^ 我想这样做

 LengthFragment l1= new LengthFragment(context,getLayoutInflater());
 addView(l1);
我的lengthfragment类将自己关联到一个xml文件

public class LengthFragment extends View implements OnClickListener,     OnItemSelectedListener{


public LengthFragment(Context context, LayoutInflater inflater){
    super(context);
    rContext = context;
inflater.inflate(R.layout.view_length, null, false);  
我试过这么做^^,它给我的只是一个空视图。

对于这一行

 inflater.inflate(R.layout.view_length, container, false);  

什么是集装箱?它应该是新视图所需的父视图。尝试将最后一个参数设置为true(attachToRoot)

那是打字错误。容器为空,附加到根目录没有帮助