简体   繁体   English

Android ViewPager findViewById不工作 - 始终返回null

[英]Android ViewPager findViewById not working - Always returning null

public class HSPTabletTestActivity extends Activity {
private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 2;
    }

    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.lighttab;
            break;
        case 1:
            resId = R.layout.securitytab;
            break;
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);
        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

}

I have this code up there ^^ ... Pretty much taken directly from http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ ... I'm still pretty new in the world of android developement :/ 我在那里有这个代码^^ ...几乎直接来自http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizo​​ntal-view-paging/ ...我是在android开发的世界中还是相当新的:/

Now I'm trying to access a spinner control and some buttons inside the "pages". 现在我正在尝试访问微调器控件和“页面”内的一些按钮。 But findViewById keeps returning null! 但findViewById一直返回null!

I remember something about the layout doesn't exist really inside the code and has to be inflated first, which is happening in the instantiateItem() function. 我记得有些布局在代码中并不存在,并且必须首先进行膨胀,这在instantiateItem()函数中发生。 And it's pretty obvious that it's going into the "view" variable. 而且很明显它会进入“视图”变量。 But when I call view.findViewById(R.id.light1_off); 但是当我调用view.findViewById(R.id.light1_off)时;

which is a button by the way, it is always returning ZERO! 顺便说一句,这是一个按钮,它总是返回ZERO! I even made sure it only called when it was actually that page being loaded. 我甚至确定只在实际加载页面时调用它。 But no matter what I did, it always keep returning a null and I get a nasty nullpointer exception. 但无论我做了什么,它总是保持返回null并且我得到一个令人讨厌的nullpointer异常。

Could someone help me out here? 有人可以帮帮我吗? I'm pretty much out of ideas, and Google isn't of any help, already been to page 5 on about 10 different search terms. 我几乎没有任何想法,谷歌没有任何帮助,已经在大约10个不同的搜索条件的第5页。

After much frustration and pulling my hair out over this issue, I have solved it! 经过多次挫折并在这个问题上拉扯我的头发,我已经解决了! At least for me. 至少对于我来说。 Assuming you used the tutsplus tutorial like I did, you have separate XML files for your screens. 假设您像我一样使用了tutsplus教程,那么您的屏幕就有单独的XML文件。 Now, I assume those layout XMLs contain layouts within them (ie. LinearLayout, RelativeLayout, etc.). 现在,我假设这些布局XML包含其中的布局(即LinearLayout,RelativeLayout等)。 Now, those layouts contain your button and other widgets. 现在,这些布局包含您的按钮和其他小部件。 What you have to do to be able to findViewById is, in the actual switch statement in the instatiateItem method, initialize your button in this way: 你需要做的是能够findViewById,在instatiateItem方法的实际switch语句中,以这种方式初始化你的按钮:

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.lighttab;
        View view = inflater.inflate(resId, null);
        RelativeLayout layout=(RelativeLayout)view.findViewById(R.id.relLayout);
        Button button=(Button)layout.findViewById(R.id.button);
        ((ViewPager) collection).addView(view, 0);
        return view;
    case 1:
        resId = R.layout.securitytab;
        ...
        ...
        return view;
    }
}

So...first you have to inflate the view, then initialize the layout held within the view by casting the type of layout (RelativeLayout) and calling .findViewById(resource id). 所以......首先你必须给视图充气,然后通过强制转换布局类型(RelativeLayout)并调用.findViewById(资源id)来初始化视图中保存的布局。 Then, you initialize the actual widget you're looking for by doing the same, but casting the widget type (Button) and calling .findViewById(resource id). 然后,通过执行相同操作来初始化您正在寻找的实际窗口小部件,但是转换窗口小部件类型(Button)并调用.findViewById(资源ID)。

This worked for me, so I hope this saves you some trouble! 这对我有用,所以我希望这能为你节省一些麻烦! Took forever for me to figure out. 永远让我弄清楚。

你可以在findViewById之前添加setContentView(R.layout.youlayout)来避免所有这些脏工作

Just guess as you don't show xml for inflated. 只是猜测你没有显示膨胀的xml。 Do you use +@id. 你使用+ @ id。 If not give it a try. 如果不试一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM