简体   繁体   English

为什么应用启动后立即消失

[英]why an app immediately disappears when launched

I am creating basic app that is drawing squares in the screen. 我正在创建在屏幕上绘制正方形的基本应用程序。 Everytime after launching my app disappears instantly. 每次启动后,我的应用都会立即消失。 Here is my code: 这是我的代码:

I have deleted auto created methods such as onCreateOptionsMenuSelected in MainActivity 我已经删除了自动创建的方法,例如MainActivity中的onCreateOptionsMenuSelected

public class MainActivity extends AppCompatActivity {
public static int WIDTH;
{
    WIDTH = getWindowManager().getDefaultDisplay().getWidth();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View customView=new CustomView(this);
    customView.setBackgroundColor(Color.BLACK);
    setContentView(customView);
}
}

And the next class 下课

public class CustomView extends View {
public static final int ELEMENTS=15;
private Paint paint=new Paint();
int x = 50;
int y = 50;

/////////////////////////////////////////////////////////
public CustomView(Context context){
    super(context);
}
////////////////////////////////////////////////////////////////////
@Override
public void onDraw(Canvas canvas){
    paint.setColor(Color.BLACK);
    canvas.drawRect(30, 30, 80, 80, paint);  //some arbitrary numbers

}
}

replace: 更换:

public static int WIDTH;{
WIDTH = getWindowManager().getDefaultDisplay().getWidth();
}

with: 与:

public int width(){
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int width = displayMetrics.widthPixels;
    return width;
}

Or remove it. 或将其删除。

and also try changing either you paint or customView background color because they are both set to BLACK 并尝试更改您的绘画或customView背景颜色,因为它们都设置为BLACK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM