activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/root_view"
>
<com.study.jy.views.ButtonGridView
android:id="@+id/btn_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</com.study.jy.views.ButtonGridView>
</LinearLayout>
MainActivity.java: method1:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = View.inflate(this, R.layout.activity_main, null);
setContentView(rootView);
btnGrid = (ButtonGridView)rootView.findViewById(R.id.btn_grid);
System.out.println(btnGrid.debug_info.toString());
}
method2
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGrid = (ButtonGridView)findViewById(R.id.btn_grid);
System.out.println(btnGrid.debug_info.toString());
}
ButtonGridView.java:
public class ButtonGridView extends GridLayout {
public static ButtonGridView myBtnGrid;
public StringBuilder debug_info = new StringBuilder();
private final String[] BTN_NAMES = {"Action Bar", "","",
"","","",
"","",""};
private Button[] Btns = new Button[9];
public ButtonGridView(Context context) {
super(context);
myBtnGrid = this;
iniView();
}
public ButtonGridView(Context context, AttributeSet attrs) {
super(context, attrs);
myBtnGrid = this;
iniView();
}
public ButtonGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
myBtnGrid = this;
iniView();
}
//Set elementary arguments in GridLayout
private void iniView(){
setAlignmentMode(GridLayout.ALIGN_BOUNDS);
setColumnCount(3);
setRowCount(3);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int itemWidth = w / 3;
int itemHeight = h / 3;
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
Button btn = new Button(getContext());
btn.setText(BTN_NAMES[i * 3 + j]);
Btns[i * 3 + j] = btn;
debug_info.append("btn added\n");
addView(btn, itemWidth, itemHeight);
}
}
}
public Button getButtonAt(int index){
if(index < 10 && index > 0)
return Btns[index - 1];
else
return null;
}
}
ButtonGridView is a custom view whose parent is GridViewLayout, I am sure that I have overridden the three costructors of GridLayout and call the super() at first.But using the 2 methods above, I just got a null reference.
After that, I tried adding a static field in ButtonGridView class which will be initialized to the instance itself in constructor, but still I got null when I called ButtonGridView.myBtnGridView(public static).
It is still confusing me, hope someone may tell me why?
update: The entire Activity:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
private ButtonGridView btnGrid = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = View.inflate(this, R.layout.activity_main, null);
setContentView(rootView);
ButtonGridView.myBtnGrid.getButtonAt(1)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}