我一直在开发一个需要查找不同设备的高度和宽度的应用程序。如果我删除高度和宽度的代码,如果我包含应用程序崩溃的代码,则应用程序可以工作。
你能建议代码有什么问题吗?
这是代码:
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;
}
}