首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findViewById返回null

findViewById返回null
EN

Stack Overflow用户
提问于 2013-12-20 19:53:39
回答 1查看 512关注 0票数 0

我在使用findViewById时遇到了问题。它返回null。我正在创建一个游戏,我有一个活动,这是我的菜单。从那里我创建并启动了我的SurfaceView。但我无法使用TextView获得对FPS findViewById的引用。

代码语言:javascript
复制
public class SplashScreen extends Activity {

    .
    .
    .

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_screen_layout);

        .
        .
        .

    }

    private void prepareButtonListeners() {
        this.mStartButton.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                setContentView(R.layout.road_view_layout);
            }
        });
        .
        .
        .


<?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" >

    <br.com.roadrun.RoadView
        android:id="@+id/road_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
        android:id="@+id/fps_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    </RelativeLayout>

</FrameLayout>


public class RoadView extends SurfaceView implements SurfaceHolder.Callback {

    private Context mContext = null;
    private SurfaceHolder mSurfaceHolder = null;
    private TextView mFPSTextView = null;

    private RoadThread mThread = null;

    public RoadView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.mContext = context;
        this.mSurfaceHolder = getHolder();
        this.mSurfaceHolder.addCallback(this);

        // THIS IS WHERE THE FINDVIEWBYID IS RETURNING NULL
        this.mFPSTextView = (TextView) findViewById(R.id.fps_text_view);

        setFocusable(true);

        this.mThread = new RoadThread(this);
    }

你有男人有这方面的线索吗?构造函数完成后,我已经尝试在另一个方法中调用findViewById,但没有成功。谢谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-20 19:59:06

视图的findViewById是在中搜索给定视图的子视图,而不是在活动的布局中。所以你应该叫活动的findViewById

代码语言:javascript
复制
((Activity) getContext()).findViewById(R.id.fps_text_view)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20711006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档