APP Stopping to work

Asked

Viewed 46 times

0

What’s wrong with the code? In the IDE it appears that there are no errors but when I open the app it closes and appears the message "app stopped".

 public class BallControl extends Activity implements SensorEventListener {
    protected Sensor s;

    private float[] gravity = new float[] {0, 0};
    private BallControlView ballView;
    private float sensorX, sensorY;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);


        SensorManager sm = (SensorManager) getSystemService (Context.SENSOR_SERVICE);
        s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if(s != null){
            ballView = new BallControlView(this);
            int ballView = 0;
            setContentView(ballView);
        }
    }

    @Override
    protected void onResume(){
        ballView.resume();
        super.onResume();}

    @Override
    protected void onPause(){
        ballView.pause();
        super.onPause();}

    @Override
    public void onSensorChanged(SensorEvent sensorEvent){

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i){

    }

    public class BallControlView extends SurfaceView implements Runnable{
        Thread thread = null;

        Bitmap ball;
        SurfaceHolder surface;
        float speedX = 0, speedY = 0;
        float positionX =0, positionY =0;
        float mass;
        final float maxSpeed= 40f;
        final double maxSpeedXY = Math.sqrt(2) * maxSpeed;
        boolean enableSoundFX;

        MediaPlayer mp;

        boolean isRunning;

        public BallControlView (Context context){
            super(context); 

            setKeepScreenOn(true);
            ball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            surface = getHolder();
            mp = MediaPlayer.create(context, R.raw.drum_0); }

        @Override
        public void run(){
            while(isRunning){
                if(surface.getSurface().isValid()){

                    Canvas canvas = surface.lockCanvas();
                    if(canvas != null){

                        float validWidth = getWidth() - ball.getWidth();
                        float validHeigth = getHeight() - ball.getHeight();

                        enableSoundFX = true;
                        mass = 30;

                        boolean collided = false;

                        speedX += sensorX/mass;
                        speedX = constrain(-maxSpeed, speedX, maxSpeed);
                        positionX = positionX + speedX;

                        if(positionX <= 0 || positionX >= validWidth) {
                            speedX = - .8f * speedX;
                            collided = true;}

                        positionX = constrain(0, positionX, validWidth);
                        speedY += sensorY/mass;
                        speedY = constrain(-maxSpeed, speedY, maxSpeed);
                        positionY = positionY + speedY;

                        if(positionY <= 0 || positionY >= validHeigth) {
                            speedY = - .8f * speedY;
                            collided = true;}

                        positionY = constrain(0, positionY, validWidth);
                        canvas.drawColor(Color.BLACK);
                        canvas.drawBitmap(ball, (int) positionX, (int) positionY, null);
                        surface.unlockCanvasAndPost(canvas);

                        if(enableSoundFX && collided){
                            float volume = (float)(Math.pow(Math.hypot((double) speedX, (double) speedY)/maxSpeedXY, 2));
                            mp.setVolume(volume, volume);

                            if(mp.isPlaying())
                                mp.seekTo(0);

                            else
                                mp.start();
                        }
                    }
                }
            }
        }


        private float constrain(float min, float value, float max){
            return Math.max(Math.min(value, max), min);}
        public void resume(){

            isRunning = true;
            thread = new Thread (this);
            thread.start();}

        public void pause(){
            isRunning = false;

            try{
                thread.join();} 
            catch(InterruptedException e){
                e.printStackTrace();}
            finally{
                thread = null;
            }
        }
    }
}
  • The mistake is probably that you are trying to use a class as the main view, what should be done is you make your layout on XML and from him doing what he wants in the code, I’m not sure if that’s it but it seems to me to be strange in the code

  • At a glance at your logcat, the error is being reported there, use the filter to show only logs from the app and if you are having trouble finding add the log to your question.

  • But how I use logCat to find the error

  • @Daniel, Follow a link with the android own tutorial who can help you, look for Logcat

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.