简单的Android应用程序在调用按钮方法时崩溃

简单的Android应用程序在调用按钮方法时崩溃,android,Android,当我尝试使用button方法并在android emulator上测试它时,它总是崩溃。如果我不调用任何方法,它首先会工作,但我需要在编程时编辑它。一个简单的例子是test1,如果我注释掉test1.getHeight(),它工作得很好,但是当我使用它时,它崩溃了。最终,我希望能够编辑高度/宽度 public class MainActivity extends Activity { final Button [] mainMenuBtn = new Button[10]; Bu

当我尝试使用button方法并在android emulator上测试它时,它总是崩溃。如果我不调用任何方法,它首先会工作,但我需要在编程时编辑它。一个简单的例子是test1,如果我注释掉test1.getHeight(),它工作得很好,但是当我使用它时,它崩溃了。最终,我希望能够编辑高度/宽度

public class MainActivity extends Activity {
    final Button [] mainMenuBtn = new Button[10];
    Button test1 = new Button(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }


        int windowWidth = this.getResources().getDisplayMetrics().widthPixels;
        int windowHeight = this.getResources().getDisplayMetrics().heightPixels;

        test1 =  (Button)findViewById(R.id.button0);
        test1.getHeight();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}
我换了它,它还是不起作用。这是我的xml

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridLayout0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="4"
     >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_columnSpan="1"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_column="1"
        android:layout_row="0"
        android:layout_columnSpan="3"
        android:src="@drawable/ic_launcher" />

</GridLayout>

如果它在调用
getHeight()
时崩溃,可能意味着
test1
null
。如果它不存在于您的内容布局中(在本例中为
activity\u main
),则它将为空。另外,请记住,按钮在创建时还没有测量,这将在以后发生


查看此SO帖子:

您可能太早调用了
getHeight

按钮
在呈现
视图
之后填充,并且在呈现
视图
之前调用您的
按钮


尝试用不同的方法移动您的呼叫,
getHeight
,而不是
onCreate

您尝试在
onCreate(Bundle savedInstanceState)
中获取按钮的
getHeight()
,但UI尚未在屏幕上调整大小和布局。因此,问题在于制造。 试试看

另一种方式

ViewTreeObserver viewTreeObs = button.getViewTreeObserver();
viewTreeObs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        height = test1.getHeight(); 
    }
});

你的activity_main.xml布局中有什么?我尝试了这个,但仍然不起作用,我将其更改为setBackgroundColor方法。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   height = test1.getHeight();
}
ViewTreeObserver viewTreeObs = button.getViewTreeObserver();
viewTreeObs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        height = test1.getHeight(); 
    }
});