我在 android 中创建了一个在实例化时创建 LinearLayouts 的新类。但是我无法弄清楚要放在括号中的上下文:new LinearLayout(context)
。有人可以阐明一下吗?(我已经尝试在上下文中阅读我所能阅读的所有内容)
我假设我不需要在课堂上扩展 Activity
public class NewLayouts {
...
newParentLayout = new LinearLayout(getApplicationContext()); //<--eclipse warns of error here saying not a valid context
newParentLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
newParentLayout.setOrientation(LinearLayout.VERTICAL);
TextView monthDisplay = new TextView(getApplicationContext()); //<--eclipse warns of error here saying not a valid context
...
}
我的主要活动:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NewLayouts Sample = new NewLayouts(1,2); //variables required in my constructor for new Layouts
setContentView(Sample.newParentLayout);
}