0

When I try to use the button method and test it on the android emulator, it always crashes. If I don't call any methods its work first, but I need to edit it when I program. A simple example is test1, if I comment out test1.getHeight() it works fine, but when I use it, it crashes. Ultimately, I want to be able to edit the height/width.

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

}

I changed it and it still didnt work. This is my 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>

And this is the code, it crashes if I call any method, but it doesnt crash when I new it or add the ID. Its really straight forward, I don't know why it causes this, perhaps its because i using the newest SDK?

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.os.Build;

public class MainMenu extends Activity {
    ImageView checkBox1;
    ImageButton checkButton1 = new ImageButton(this);

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

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

         checkBox1 = new ImageView(this);
        checkBox1 = (ImageView)findViewById(R.id.imageView1);
        checkButton1 = (ImageButton)findViewById(R.id.imageButton1);

        final ViewTreeObserver viewTreeObs = checkBox1.getViewTreeObserver();
        viewTreeObs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                checkBox1.setBackgroundColor(Color.RED);
                viewTreeObs.removeGlobalOnLayoutListener(this);
            }
        }); 

        //


    }

    @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, 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_menu,
                    container, false);
            return rootView;
        }
    }

}
GoChanGo
  • 61
  • 1
  • 1
  • 8

3 Answers3

1

You may be calling getHeight too early.

Button is populated after the View is rendered, and your Button is called before View is rendered.

Try move your call, getHeight around in different methods, other than onCreate.

melvynkim
  • 1,655
  • 3
  • 25
  • 38
1

You are trying to get getHeight() of a button in onCreate(Bundle savedInstanceState) but UI has not been sized and laid out on the screen yet. So the problem is creating. Try it

@Override
public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   height = test1.getHeight();
}

Another Way

ViewTreeObserver viewTreeObs = button.getViewTreeObserver();
viewTreeObs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        height = test1.getHeight(); 
    }
});
user3384985
  • 2,975
  • 6
  • 26
  • 42
0

If it's crashing when calling getHeight() it probably means that test1 is null. It would be null if it doesn't exist in your content layout, which in this case is activity_main. Also, keep in mind that the button hasn't been measured yet on creation, that happens later on.

See this SO post: getHeight returns 0 for all Android UI objects

Community
  • 1
  • 1
yincrash
  • 6,394
  • 1
  • 39
  • 41