我正在尝试将按钮添加到线性布局:
public void populateAlarmLayout() {
Log.d("Array", alarmButtons.size() + "");
alarmLayout = (LinearLayout)findViewById(R.id.alarmlayout);
for(int i = 0; i < alarmButtons.size(); i++) {
selectedButton = new Button(this);
selectedButton.setId(i+1);
selectedButton.setWidth(150);
selectedButton.setHeight(150);
selectedButton.setTextSize(10);
selectedButton.setText(alarmButtons.get(i));
selectedButton.setOnClickListener(new ClickListener());
selectedButton.setOnLongClickListener(new LongClickListener());
alarmLayout.addView(selectedButton);
}
}
nullpointer
我在最后一行不断收到异常alarmLayout.addView(selectedButton)
The alarmlayout
is specified inalarm.xml
但是我的当前contentView
设置为main_activity.xml
. 我很确定这是问题所在,但我不知道如何解决它!
我正在使用,PagerAdapter
所以这也可能是问题所在。这是代码:
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.main;
break;
case 1:
resId = R.layout.music;
break;
case 2:
resId = R.layout.alarm;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
}