0

我一直在开发一个需要查找不同设备的高度和宽度的应用程序。如果我删除高度和宽度的代码,如果我包含应用程序崩溃的代码,则应用程序可以工作。

你能建议代码有什么问题吗?

这是代码:

public class Newone extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newone);
    }

    final Context context = this;
    public String cntry = "";
    int w= getWindowManager().getDefaultDisplay().getWidth();
    int h = getWindowManager().getDefaultDisplay().getHeight();

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int w = metrics.widthPixels;
    int h = metrics.heightPixels;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        MotionEvent object holds X-Y values
        if(event.getAction() == MotionEvent.ACTION_DOWN) {              
            String text = "You click at x = " + event.getX() + " and y = " + event.getY();
            //Toast.makeText(this, text, Toast.LENGTH_LONG).show();

            if((event.getX()>((w/1280)*850)) && (event.getX()<((h/752)*900)) && (event.getY()>((w/1280)*380)) && (event.getY()<((h/752)*450)))
                if((event.getX()>850) && (event.getX()<900) && (event.getY()>380) && (event.getY()<450)) {
                    Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                    display(cntry);
                }

                if((event.getX()>280) && (event.getX()<400) && (event.getY()>200) && (event.getY()<300)) {
                    //Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                    cntry = "US";
                    display(cntry);
                }

                if((event.getX()>900) && (event.getX()<1200) && (event.getY()>500) && (event.getY()<650))
        }
        return super.onTouchEvent(event);
    }   

    private void display(final String cntry) {
        final Dialog dialog = new Dialog(context);
        dialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_newone, menu);
        return true;
    }

}
4

2 回答 2

1

您必须在 onCreate 中移动以下内容。

final Context context = this;
public String cntry = "";


DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int w = metrics.widthPixels;
int h = metrics.heightPixels;

此外getWindowManager().getDefaultDisplay().getWidth();getHeight现在已弃用..所以避免使用它们。

这是更正的片段。

public class Newone extends Activity {
    final Context context;
    public String cntry = "";
    int w, h;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newone);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        context = this;

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        w = metrics.widthPixels;
        h = metrics.heightPixels;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            String text = "You click at x = " + event.getX() + " and y = "
                    + event.getY();
            // Toast.makeText(this, text, Toast.LENGTH_LONG).show();

            if ((event.getX() > ((w / 1280) * 850))
                    && (event.getX() < ((h / 752) * 900))
                    && (event.getY() > ((w / 1280) * 380))
                    && (event.getY() < ((h / 752) * 450)))
                if ((event.getX() > 850) && (event.getX() < 900)
                        && (event.getY() > 380) && (event.getY() < 450))

                {
                    Toast.makeText(this, text, Toast.LENGTH_LONG).show();

                    display(cntry);
                }

            if ((event.getX() > 280) && (event.getX() < 400)
                    && (event.getY() > 200) && (event.getY() < 300))

            { // Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                cntry = "US";
                display(cntry);
            }

            if ((event.getX() > 900) && (event.getX() < 1200)
                    && (event.getY() > 500) && (event.getY() < 650)) {

            }

        }

        return super.onTouchEvent(event);
    }

    private void display(final String cntry) {

        final Dialog dialog = new Dialog(context);
        dialog.show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_newone, menu);
        return true;
    }

}
于 2012-07-18T07:53:15.943 回答
0

如果你搬家了怎么办getWindowManager().getDefaultDisplay().getWidth();和你getHeight()metrics = context.getResources().getDisplayMetrics();内心onCreate?我不确定context,其他人在Activity's 的施工时间可用。

于 2012-07-18T07:49:16.883 回答