为什么我在Android中的setContentView()之后无法获得自定义视图引用?

时间:2015-03-23 01:52:51

标签: android

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: 方法1:

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());
   }

方法2

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 是一个自定义视图,其父级为 GridViewLayout ,我确信我已覆盖 GridLayout <的三个总结构< / strong>并首先调用super()。但是使用上面的两种方法,我得到了一个 null 引用。

之后,我尝试在 ButtonGridView 类中添加一个静态字段,它将在构造函数中初始化为实例本身,但仍然得到 null 当我调用 ButtonGridView.myBtnGridView (公共静态)时。

我仍然困惑,希望有人可以告诉我为什么?

更新: 整个活动:

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

                  }     
               });
    }
}

1 个答案:

答案 0 :(得分:0)

如果ButtonGridView是自定义Button,那么它应该是这样的。

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

                  }     
               });
    }
}