1

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) {

                  }     
               });
    }
}
Tommy
  • 301
  • 2
  • 16
  • `MainActivity.java: method1:` is flat out wrong. Can you post both sources? – Jared Burrows Mar 23 '15 at 02:19
  • Any reason you are trying to get your Views in onCreate() and not onCreateView()? Is there a functional requirement? – fakataha Mar 23 '15 at 02:33
  • @atamakosi His is using an `Activity`. – Jared Burrows Mar 23 '15 at 02:36
  • The activity displays in a right way, method1 and method2 are just ways I tried to get the reference.(MainActivity extends ActionBarActivity with only one method onCreate()).Can you tell me why method1 is totally wrong? – Tommy Mar 23 '15 at 02:38
  • @JaredBurrows Using an Activity doesn't require that you hook into your views in onCreate(). (which is why we have onCreateView(), just think its cleaner to separate methods appropriately.) – fakataha Mar 23 '15 at 02:41
  • @atamakosi No tutorial ever suggests `onCreateView()`. I'd suggest you read more here: http://stackoverflow.com/questions/24156926/oncreateview-method-gets-called-when-and-how-many-times-in-activity-life-cycle. – Jared Burrows Mar 23 '15 at 02:45
  • @JY___ Post your entire `Activity`. – Jared Burrows Mar 23 '15 at 02:45
  • @JaredBurrows What I want is to get the custom view reference then do something on the views inside the custom view. – Tommy Mar 23 '15 at 03:10
  • Why are you using `View.inflate` and `getButtonAt`? – Jared Burrows Mar 23 '15 at 03:21
  • @JaredBurrows Because I have tried to use **findViewById()** after setContentView(), and got a **null**, then I tried this way.**getButtonAt()** is a method of the custom view, it has an array to save the references of the created views, so I use this method to get the view inside the custom view.I am new to android, can you tell me what is a proper way to do this? – Tommy Mar 23 '15 at 03:26
  • I thought `ButtonGridView` was a `Button` that was custom. – Jared Burrows Mar 23 '15 at 03:32
  • Why are you extending `GridLayout`? – Jared Burrows Mar 23 '15 at 03:34

1 Answers1

0

If ButtonGridView was a custom Button, then it should look like this.

public class MainActivity extends ActionBarActivity {
    private ButtonGridView btnGrid = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnGrid = (ButtonGridView)findViewById(R.id.btn_grid);

       myBtnGrid.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {

                  }     
               });
    }
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • I thought `ButtonGridView` was a `Button` that was custom. – Jared Burrows Mar 23 '15 at 03:32
  • **ButtonGridView** is a **GridLayout** having 9 **Buttons**.And I tried what you post here, the **btnGrid** reference isn't null, but if I print the debug_info.toString() (A StringBuilder in ButtonGridView), still I got nothing there.That means the **btnGrid** isn't the view displaying on the screen.I don't know why. Thanks a lot for your patience. – Tommy Mar 23 '15 at 03:46
  • Why not use XML? Do not extend the class: http://code.tutsplus.com/tutorials/android-user-interface-design-creating-a-numeric-keypad-with-gridlayout--mobile-8677. Also, I am not sure `onSizeChanged` is called like you want: http://developer.android.com/reference/android/view/View.html#onSizeChanged(int, int, int, int). – Jared Burrows Mar 23 '15 at 03:51
  • I just print the debug log and found something useful.The **Buttons** created **after** I had got the reference of **ButtonGridView** in **onCreate()**.So that I can not reach the views inside the custom view when I try to get them in the **onCreate()** method.I think that is about how the Android system draws those views. – Tommy Mar 23 '15 at 04:16
  • Since you are new, just follow the tutorial because you know it works. – Jared Burrows Mar 23 '15 at 04:18