从 onResume() 方法访问 Viewpager 中的 TextView.setText()

发布于 2024-12-13 06:14:29 字数 2677 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唐婉 2024-12-20 06:14:29

Name 必须设置为 null...您的应用在初始化之前必须已暂停。在尝试对它执行任何操作之前,只需检查以确保它已初始化。 onResume() 应该如下所示:

@Override
protected void onResume() {
  super.onResume();
  if(Name != null){
    Name.setText("1");
  }
}

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're onResume() should look like this then:

@Override
protected void onResume() {
  super.onResume();
  if(Name != null){
    Name.setText("1");
  }
}

P.S. Standard java convention is to have all variable names begin with a lower case letter. You should probably rename Name to name.

纵山崖 2024-12-20 06:14:29

首先,在您的代码中,您没有获取对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文