创建imagebutton报错空指针,代码如下,请前辈帮忙看看
import android.content.Context;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
public class PlatformPage extends TableLayout {
private ImageButton[] imageButton;
public PlatformPage(Context context) {
super(context);
}
public void show() {
buttonInit();
addImageButton();
}
public void buttonInit() {
// weiimageButton设置各个平台的背景
for (int i = 0; i < 6; i++) {
imageButton[i] = new ImageButton(this.getContext());
}
imageButton[0].setBackground(getResources().getDrawable(
R.drawable.qzone_116));
imageButton[1].setBackground(getResources().getDrawable(
R.drawable.sinaweibo_116));
imageButton[2].setBackground(getResources().getDrawable(
R.drawable.renren_116));
imageButton[3].setBackground(getResources().getDrawable(
R.drawable.tecentweibo_116));
imageButton[4].setBackground(getResources().getDrawable(
R.drawable.weixin_116));
imageButton[5].setBackground(null);
for (int i = 0; i < 6; i++) {
imageButton[i].setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageButton[i].setPadding(5, 5, 5, 5);
}
}
}
报错提示我是imageButton[i]=new ImageButton(getContext())出错了
------解决方案--------------------
数组没有初始化的问题,你的数组有6个元素,private ImageButton[] imageButton=new ImageButton[6],肯定不会有错了