headSp.spriteFrame.setTexture(texture)为什么会报错

var headSp = headpic.addComponent(cc.Sprite);
var imgPath = “https://api.17gulu.com”+dataJson[i].photo_local;
cc.loader.load(imgPath,function(err, texture){
headSp.spriteFrame.setTexture(texture);
})
imgPath可以确定正确,而用texture也可以正确获取,headSp.spriteFrame.setTexture这个会报错

Cannot read property ‘setTexture’ of null;at setTimeout callback function
TypeError: Cannot read property ‘setTexture’ of null

你的spriteFrame本来就是null, null.setTexture是不行的

改为headSp.spriteFrame = new cc.SpriteFrame(texture)

可以了,感谢大神