我试图通过其ID来呼叫LinearLayout
。当我试图这样做时,我得到了NoSuchFieldError
。
LinearLayout l1 = (LinearLayout)findViewById(R.id.content2);
setContentView(l1);
答案 0 :(得分:1)
您使用的方式不正确。
setContentView(R.layout.main)必须设置任何布局,例如你的案例的main.xml。
现在主要布局是具有id content2的LinearLayout。
另外,如果你想直接使用setContentView创建一个动态线性布局,即不在xml中。
Linearlayout l1 = new LinearLayout(this);
//Set Layout params and bla bla... as per your need..
now setContentView(l1);
答案 1 :(得分:0)
当且仅当您在xml资源文件中提供了正在膨胀的ID时,才能找到该ID。如果不加载xml资源文件,则无法找到id。你可以使用Activity onCreate()中的setContentView(R.layout.main);
加载xml资源文件。查找ID的代码将类似于LinearLayout l = findViewById(R.id.content);