为什么方法getSize()在Android Studio中不起作用?

时间:2015-05-27 17:03:33

标签: java android

我目前正在尝试运行代码以获取我在Android Studio中使用的屏幕的宽度和高度。 (以下代码) 除了getSize()之外,一切都在我的代码中工作;方法。我正在运行Api等级22,最低等级为15。

编辑:我刚刚意识到它可能与“扩展ActionBarActivity”有关。而不只是扩展“活动”。这会有影响吗?

以下代码片段不起作用:

EOT

以下是所有代码:

public Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size); //error here with getSize not being recognized.
screenWidth = size.x;
screenHeight = size.y;

这是我的Api等级:

package com.example.e99900004533.candycollector;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.graphics.Point;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Display;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.RelativeLayout;
import android.widget.EditText;
import android.widget.ImageButton;
import java.util.Random;

public class MainActivity extends ActionBarActivity {

    public EditText collectedTextEdit;
    public ImageButton candyEdit;
    public int collected = 0;
    public int screenWidth = 300;
    public int screenHeight = 300;
    public final int candySize = 50;
    Random random = new Random();

    public Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    //screenWidth = size.x;
    //screenHeight = size.y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        collectedTextEdit = (EditText) findViewById(R.id.collectedText);
        candyEdit = (ImageButton) findViewById(R.id.candy);
        collectedTextEdit.setText("Collected: " + collected);
        System.out.println(screenWidth + " : " + screenHeight);
        addListenerOnButton();
    }

    public void addListenerOnButton() {
        candyEdit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                collected += 1;
                collectedTextEdit.setText("Collected: " + collected);

                MarginLayoutParams marginParams = new MarginLayoutParams(candyEdit.getLayoutParams());
                marginParams.setMargins(random.nextInt(screenWidth - candySize), random.nextInt(screenHeight - candySize), 0, 0);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
                candyEdit.setLayoutParams(layoutParams);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:2)

你需要在你的onCreate上做到这一点

@Override
protected void onCreate(Bundle savedInstanceState) {
    display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);