【发布时间】:2014-02-19 23:09:04
【问题描述】:
我无法在 ViewPager 中设置 TextView 的 setText()。 这是我的主要活动课。我想在 ViewPager 制作的布局滑动中更新 textView。
public class MainActivity extends Activity {
ViewPager myPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(3);
}
@Override
protected void onResume() {
super.onResume();
// this code causes the app to crash, because baseLayout is null
View baseLayout = myPager.findViewWithTag(R.layout.hoteladdress);
//TextView bubu = (TextView) baseLayout.findViewById(R.id.bubu);
//bubu.setText("TEST");
}
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 = showHotelContact();
break;
case 1:
resId = showHotelAddress();
break;
case 2:
resId = showHotelMap();
break;
}
View view = (View) inflater.inflate(resId, null);
view.setTag(resId);
((ViewPager) collection).addView(view, 0);
return view;
}
}
public int showHotelMap()
{
int resId;
resId = R.layout.hotelmap;
return resId;
}
public int showHotelAddress()
{
int resId;
resId = R.layout.hoteladdress;
return resId;
}
public int showHotelContact()
{
int resId;
resId = R.layout.hotelcontact;
return resId;
}
}
请在 onResume 方法中找到我注释的行来解释问题。
【问题讨论】:
标签: android textview android-viewpager