自定义视图上的FindViewByID返回null?

时间:2011-02-03 20:05:59

标签: android

  

可能重复:
  findViewById() returns null for custom component in layout XML, not for other components

嗨! 我无法使用findViewById()访问我的自定义视图。我需要达到它的原因是因为我想从A类创建一个与B类交互的菜单。

设置: 我有两个A和B类.A扩展Activity,B扩展View。 我想从A到达B类,但是当我在B类上使用findViewById()时,它返回null。我见过类似的问题,但大多数人只是使用了错误的.xml文件。 在这里,我很确定我使用了正确的.xml文件(R.layout.main),并在main.xml中创建了我的文件 viewTest。它运行顺利,做我想做的事情,除非我想从A级到B级做一些事情。而且据我所知,我需要用A级来创建我想要的菜单。

这是来自A类的作品,其中我尝试使用findViewByID():

viewTest viewTest;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    viewTest = (ViewTest) findViewById(R.id.snakes);

}

这是.xml文件,我尝试从中获取id蛇:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<com.Test.ViewTest 
    android:id="@+id/snakes"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />


</FrameLayout>

这确实困扰着我,我认为答案就在我的鼻子底下,我只是看不到它&gt;。&lt;提前感谢任何可以让我更接近解决这个问题的答案! :d

0 个答案:

没有答案