我在使用findViewById时遇到了问题。它返回null。我正在创建一个游戏,我有一个活动,这是我的菜单。从那里我创建并启动了我的SurfaceView。但我无法使用TextView获得对FPS findViewById的引用。
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,但没有成功。谢谢,
发布于 2013-12-20 19:59:06
视图的findViewById是在中搜索给定视图的子视图,而不是在活动的布局中。所以你应该叫活动的findViewById
((Activity) getContext()).findViewById(R.id.fps_text_view)https://stackoverflow.com/questions/20711006
复制相似问题