0

here is my code:

public class MainMenuActivity extends Activity implements OnClickListener {

    public int width;
    public int height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);

        Button play = (Button) findViewById(R.id.playbut);
        Button credits = (Button) findViewById(R.id.creditsbut);
        Button help = (Button) findViewById(R.id.helpbut);

        play.setOnClickListener(this);
        credits.setOnClickListener(this);
        help.setOnClickListener(this);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        height = dm.heightPixels;       
}

the problem is, public int width is not getting being set to dm.widthPixels and likewise for height. Any tips?

3
  • They are if there's a value in the dm properties; what makes you think they're not? Commented Nov 5, 2011 at 3:34
  • 1
    Whatever your title says is not allowed in Java. Commented Nov 5, 2011 at 3:53
  • @BheshGurung - more to the point, it doesn't really relate to the question being asked. Commented Nov 5, 2011 at 4:03

3 Answers 3

2

The code as given should result in the attributes being set. If it is not happening then there must be something else going on:

  • Maybe the method is not getting called.
  • Maybe the values of the dm pixel attributes are not what you expect them to be.
  • Maybe something else is changing the width and height values after the method call.
  • ...
  • Maybe the code you are actually running doesn't correspond to the source code that you've shown us ... or the actual code that you are looking at. (For instance, you might have a problem with your build / deploy procedures.)

If you are stumped, try running using a debugger, and setting a breakpoint in the method. Single step the method, and examine the actual values being set.

Sign up to request clarification or add additional context in comments.

1 Comment

A good debugger should also be able to set a breakpoint whenever the value of a variable is changed. That will help you discover if another class is changing things underneath you (although, making the variables private is a much better way to detect and prevent this).
1

This sort of problem is why it's generally considered bad practice to have public instance variables. You're much better off making the instance variables private and have a public getter if you need it. This means you can guarantee no-one changes the value without your knowledge:

public class MainMenuActivity ... {
    private int width;
    private int height;

    public int getWidth() {
        return width;
    }

    // etc
}

If you make this change then you'll probably discover immediately who was changing the value underneath you - whatever was setting the values of width and height will now fail to compile.

Comments

0

this.width = dm.widthPixels; this.height = dm.heightPixels;

yes you can try this and if still it not working then you can try this with by changing the data type of width and height int to double. may be the limit problem. if it work then tell me.

2 Comments

Changing the data type won't affect anything. If it was a precision problem then there would be a compiler error. Adding the this will also do nothing because there is no shadowing going on here.
oh yes. i have one more idea you can create this file again and copy past the same data on that may be it work or one more thing there may be problem for referencing, i think you should restart your complete application and editor also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.