从自定义视图访问的RelativeLayout时的NullPointerException [英] NullPointerException when accessing RelativeLayout from a custom view

查看:276
本文介绍了从自定义视图访问的RelativeLayout时的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的Andr​​oid和我完全被卡住如何在我的自定义视图访问我的编程定义相对布局(在一个片段中定义)。

I am relatively new to Android and am completely stuck on how to access my programmatically-defined Relative Layout (defined in a fragment) in my custom View.

在片段,这是我有:

...
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment1, container,false);
    RelativeLayout rl1 = new RelativeLayout(view.getContext());
    TextView tView1 = new TextView(view.getContext()); 
    tView1.setText("test");
    rl1.addView(tView1); 
    rl1.setId(1);
    tView1.setId(2);
...
}

然后在自定义视图我调用由ID相对布局和TextView中。当我尝试做任何事情,我得到一个空指针异常。

Then in the custom view I call the relative Layout and TextView by id. When I try to do anything, I get a NullPointer exception.

...
RelativeLayout rl1 = (RelativeLayout) findViewById(1);
TextView tView1 = (TextView) findViewById(2);
tView1.getText();

在code上面显示试图 .getText()在TextView的,但任何事情我做了RelativeLayout的还会导致一个空指针异常。

The code above shows trying to .getText() on the TextView, but anything I do to the RelativeLayout also causes a NullPointer exception.

因此​​,基本上,它看起来像我没有正确地找到RelativeLayout的和TextViews。
仅供参考,我已经看过<一个href=\"http://stackoverflow.com/questions/12915856/android-nullpointerexception-in-custom-view-access\">this类似的问题,但它并没有适用于此处,我的构造已经建立了适当的。

So basically, it looks like I'm not finding the RelativeLayout and TextViews correctly. FYI, I've already seen this similar question, but it didn't apply here, my constructors are already set up appropriately.

推荐答案

您应该添加布局主视图,但是,你需要先投它变成一个布局:

You should be adding the Layout to the main View, however, you need to cast it first into a Layout:

YourLayout view = (YourLayout) inflater.inflate(R.layout.fragment1, container,false);
RelativeLayout rl1 = new RelativeLayout(view.getContext());
TextView tView1 = new TextView(view.getContext()); 
tView1.setText("test");
rl1.addView(tView1); 
rl1.setId(1);
tView1.setId(2);
view.addView (rl1);//add rl1 to the main View

然后访问,如果你在外面打工 onCreateView()的的 onCreateView()已经被称为:

Then to access, if you're working outside onCreateView() and onCreateView() has already been called:

RelativeLayout rl1 = (RelativeLayout) getView().findViewById(1);
TextView tView1 = (TextView) rl1.findViewById(2);
tView1.getText();

如果你还在试图访问 onCreateView() findViewById()没有意见事 - 你已经有意见的引用,以便用这些

If you're still trying to access the views in onCreateView(), findViewById() doesn't matter - you already have the references of your Views so use those.

另外值得一提的是,增加的听众和这样的 onCreateView()更新UI可以做到,所以你可以简单地事情了一下。

It's also worth noting that adding listeners and such in onCreateView() to update the UI can be done, so you may be able to simply things a bit.

这篇关于从自定义视图访问的RelativeLayout时的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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