findViewById返回null

findViewById返回null网上一般都有那2种可能错误的指正,1、在setContentView之前调用了;2、调用LayoutInflater实例化的view1,然后却没有用view1.findViewById而是直接fidnViewById。

但我今天也出了这么个错误,不是这2种情况,而是:

	public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
	}

	public MySurfaceView(Context context, AttributeSet attrs) {
		this(context, null, 0);
		// TODO Auto-generated constructor stub
	}

	public MySurfaceView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

原本这样设计是为了无论是哪个构造方法,都会走到第一个构造方法去,所以当我在其他地方findViewById这个view的时候,就报空指针了。原来是:

	public MySurfaceView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}


这里由于疏忽写了个null,改了就好了。

小问题,记录下来。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值