从 onResume() 方法访问 Viewpager 中的 TextView.setText()
我对 Android 开发相当陌生,所以我遇到了 onResume() 方法的问题。
当我尝试从 onResume() 方法中编写 Name.setText() 时,我的代码崩溃了。 从 onPause() 方法中编写 TextView 的效果令人惊讶,没有任何问题。
如何从 onResume() 方法中访问此 TextView?
public class Main extends Activity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 2;
private Context cxt;
private AwesomePagerAdapter awesomeAdapter;
View v;
TextView Name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cxt = this;
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
}
@Override
protected void onResume() {
super.onResume();
Name.setText("1");
}
@Override
protected void onPause() {
super.onPause();
}
private class AwesomePagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
View v = new View(cxt.getApplicationContext());
final LayoutInflater inflater = (LayoutInflater) cxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (position) {
case 0:
v = inflater.inflate(R.layout.subpage1, null, false);
Name = (TextView) v.findViewById(R.id.name);
break;
case 1:
v = inflater.inflate(R.layout.subpage2, null, false);
break;
default:
break;
}
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView(v);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
}
I'm fairly new to Android development so I ran into a problem with the onResume() method.
My code crashes when I try to write Name.setText() from within the onResume() method.
Writing the TextView from within the onPause() method works surprisingly without any problem.
How can I access this TextView from within the onResume() method?
public class Main extends Activity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 2;
private Context cxt;
private AwesomePagerAdapter awesomeAdapter;
View v;
TextView Name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cxt = this;
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
}
@Override
protected void onResume() {
super.onResume();
Name.setText("1");
}
@Override
protected void onPause() {
super.onPause();
}
private class AwesomePagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
View v = new View(cxt.getApplicationContext());
final LayoutInflater inflater = (LayoutInflater) cxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (position) {
case 0:
v = inflater.inflate(R.layout.subpage1, null, false);
Name = (TextView) v.findViewById(R.id.name);
break;
case 1:
v = inflater.inflate(R.layout.subpage2, null, false);
break;
default:
break;
}
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView(v);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Name
必须设置为 null...您的应用在初始化之前必须已暂停。在尝试对它执行任何操作之前,只需检查以确保它已初始化。onResume()
应该如下所示:PS 标准 java 约定是让所有变量名称以小写字母开头。您可能应该将
Name
重命名为name
。Name
must be set to null...Your app must have paused before it ever got initialized. Just check to make sure it's initialized before trying to do anything with it. You'reonResume()
should look like this then:P.S. Standard java convention is to have all variable names begin with a lower case letter. You should probably rename
Name
toname
.首先,在您的代码中,您没有获取对
Name
文本视图的引用,因此您一定会收到 NullPointerException。如果你解决了这个问题,我认为它应该有效。如果没有,请告诉我们您得到的异常名称。
First of all, in your code you don't acquire a reference to the
Name
text view, so you must be getting NullPointerException.If you fix that, it should work IMO. If it does not, tell us the exception name you get.