Android的ViewPager findViewById不工作 - 总是返回null [英] Android ViewPager findViewById not working - Always returning null

查看:238
本文介绍了Android的ViewPager findViewById不工作 - 总是返回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

    }

}

我有这个code在那里^^ ... pretty的多少直接从<一个href="http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/" rel="nofollow">http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ ......我还是pretty的新的机器人研究与开发的世界:/

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 :/

现在我正在试图访问里面的页微调控件和一些按钮。但findViewById保持返回空值!

Now I'm trying to access a spinner control and some buttons inside the "pages". But findViewById keeps returning null!

我还记得一些关于布局不里面的code确实存在,并且必须先膨胀,这是发生在instantiateItem()函数。而且它是pretty的明显,它会进入视图变量。但是,当我打电话     view.findViewById(R.id.light1_off);

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. And it's pretty obvious that it's going into the "view" variable. But when I call view.findViewById(R.id.light1_off);

其由方式的按钮时,它总是返回零! 我甚至确信它只是叫时,它实际上是该页面正在加载。但无论我做什么,它总是回头率空,我得到一个讨厌的空指针异常。

which is a button by the way, it is always returning 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.

有人能帮助我在这里?我是pretty的多出出主意,而谷歌是什么帮助没有,已经第5页上的约10个不同的搜索条件。

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.

推荐答案

在太多的无奈和拉我的头发在这个问题上,我已经解决了!至少对我来说。假设你使用的tutsplus教​​程像我一样,你有你的屏幕上单独的XML文件。现在,我想这些布局XMLS包含在其中的布局(即的LinearLayout,RelativeLayout的,等等)。现在,这些布局包含您的按钮和其他部件。你所要做的是能够findViewById是,在instatiateItem方法的实际switch语句,初始化按钮以这种方式:

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. Now, I assume those layout XMLs contain layouts within them (ie. LinearLayout, RelativeLayout, etc.). 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:

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;
    }
}

所以...首先你得膨胀的观点,然后通过铸造布局(RelativeLayout的)的类型和调用.findViewById(资源ID)初始化视图内举行的布局。然后,您初始化你正在寻找做相同的,但铸造部件类型(按钮),并调用.findViewById(资源ID)的实际部件。

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). 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).

这为我工作,所以我希望这可以节省你一些麻烦!永远了,我要弄清楚。

This worked for me, so I hope this saves you some trouble! Took forever for me to figure out.

这篇关于Android的ViewPager findViewById不工作 - 总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆