Search code examples
javaandroidxmlandroid-studiogestures

App keeps freezing,Android studio error?


In this app i am trying to get a small Button to move accross the screen, and am getting the user to try and avoid the blocks that are slowly falling across the screen.

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="dev.kdeveloper.ballrun.MainActivity"
    android:id="@+id/relativelayout">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ball"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="31dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="box1"
        android:id="@+id/box1"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="click to start"
        android:id="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="98dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Game Over"
        android:id="@+id/textView2"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Java Class:

package dev.kdeveloper.ballrun;

import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.Random;

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{//i am using gestures in my application
    GestureDetectorCompat gd;
    public int location = 2;
    ViewGroup vg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.gd = new GestureDetectorCompat(this,this);
        final TextView tv = (TextView)findViewById(R.id.textView);
        final TextView gameover = (TextView)findViewById(R.id.textView2);
        final Animation blocksfall = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.blocksfall);
        final Button b1 = (Button)findViewById(R.id.box1);
        gameover.setVisibility(View.INVISIBLE);
        tv.setVisibility(View.VISIBLE);
        gd.setOnDoubleTapListener(this);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setVisibility(View.INVISIBLE);
                mainloop(blocksfall, b1);
            }
        });

    }public boolean continue1 = false;


    private void mainloop(Animation blocksfall, Button b1) {
        final Button ball = (Button)findViewById(R.id.ball);
        Random rand = new Random();
        int loopnum = 0;
        boolean playerisplaying = true;
        while(playerisplaying){
            if(rand.nextInt(3)+1==1){
                b1.setX(200);
            }else if(rand.nextInt(3)+1==2){
                b1.setX(400);
            }else if(rand.nextInt(3)+1==3){
                b1.setX(600);
            }
            loopnum+=1;
            b1.startAnimation(blocksfall);
            b1.setY(b1.getY()-400);
            if(b1.getY()>ball.getY()+10){
                if(b1.getX()==ball.getX()){
                    ball.setText("continue");
                    continue1 = false;

                    while(continue1==false){
                        ball.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                continue1 = true;
                            }
                        });
                    }
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        final Button ball = (Button)findViewById(R.id.ball);
        Animation oneto2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.oneto2);
        Animation threeto2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.threeto2);
        Animation twoto3 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.twoto3);
        Animation twotoone = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.twoto1);
        Animation onetoone = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.onetoone);
        Animation threetothree = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.threetothree);

        if(e.getX()>400){
            if(location==2){
                location = 3;
                ball.startAnimation(twoto3);
                ball.setX(600);
            }else if(location==1){
                location = 2;
                ball.startAnimation(oneto2);
                ball.setX(400);
            }else if(location==3){
                ball.startAnimation(threetothree);
                location = 3;
                ball.setX(600);
            }
        }else if(e.getX()<400){
            if(location==2){
                location = 1;
                ball.startAnimation(twotoone);
                ball.setX(200);
            }else if(location==1){
                ball.startAnimation(onetoone);
                locat`enter code here`ion = 1;
                ball.setX(200);
            }else if(location==3){
                ball.startAnimation(threeto2);
                location = 2;
                ball.setX(400);
            }
        }
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event){

        this.gd.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

}

Animation Layout:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:duration="150"
        android:toXDelta="100"
        android:repeatCount="1"
        android:repeatMode="reverse"/>
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:duration="150"
        android:toXDelta="-100"
        android:repeatCount="1"
        android:repeatMode="reverse"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fromXDelta="200"
        android:toXDelta="0"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromYDelta="400"
        android:toYDelta="0"/>
</set>

much of the rest of the files are similar. These files animate the transition between points. The button is going to be moving between three different areas. This means that the animations aren't all that important because they work, and the app freezes up when i click on the text view. This is also when the main loop is called. Could that be the problem?

Logcat:

> 01-23 20:20:28.892 31538-31544/dev.kdeveloper.ballrun I/art:
> Thread[2,tid=31544,WaitingInMainSignalCatcherLoop,Thread*=0xaf60e400,peer=0x12c8b080,"Signal
> Catcher"]: reacting to signal 3 01-23 20:20:29.342
> 31538-31544/dev.kdeveloper.ballrun I/art: Wrote stack traces to
> '/data/anr/traces.txt'

entire logcat:
01-23 20:19:54.552 31538-31538/? E/Zygote: MountEmulatedStorage()
01-23 20:19:54.552 31538-31538/? E/Zygote: v2
01-23 20:19:54.552 31538-31538/? I/libpersona: KNOX_SDCARD checking this for 10360
01-23 20:19:54.552 31538-31538/? I/libpersona: KNOX_SDCARD not a persona
01-23 20:19:54.572 31538-31538/? I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G900F_5.0 ver=27
01-23 20:19:54.572 31538-31538/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G900F_5.0-1_0032
01-23 20:19:54.572 31538-31538/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
01-23 20:19:54.572 31538-31538/? I/art: Late-enabling -Xcheck:jni
01-23 20:19:54.602 31538-31538/? D/TimaKeyStoreProvider: TimaSignature is unavailable
01-23 20:19:54.602 31538-31538/? D/ActivityThread: Added TimaKeyStore provider
01-23 20:19:54.642 31538-31538/dev.kdeveloper.ballrun D/ResourcesManager: creating new AssetManager and set to /data/app/dev.kdeveloper.ballrun-1/base.apk
01-23 20:19:54.852 31538-31538/dev.kdeveloper.ballrun V/BitmapFactory: DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi-v4/abc_ic_ab_back_mtrl_am_alpha.png
01-23 20:19:54.892 31538-31538/dev.kdeveloper.ballrun D/Activity: performCreate Call secproduct feature valuefalse
01-23 20:19:54.892 31538-31538/dev.kdeveloper.ballrun D/Activity: performCreate Call debug elastic valuetrue
01-23 20:19:54.912 31538-31583/dev.kdeveloper.ballrun D/OpenGLRenderer: Render dirty regions requested: true
01-23 20:19:54.942 31538-31583/dev.kdeveloper.ballrun I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
                                                                    OpenGL ES Shader Compiler Version: E031.25.01.03
                                                                    Build Date: 03/03/15 Tue
                                                                    Local Branch: LA.BF.1.1_RB1_20150108_025_1077123_1158499
                                                                    Remote Branch: 
                                                                    Local Patches: 
                                                                    Reconstruct Branch: 
01-23 20:19:54.942 31538-31583/dev.kdeveloper.ballrun I/OpenGLRenderer: Initialized EGL, version 1.4
01-23 20:19:54.962 31538-31583/dev.kdeveloper.ballrun I/OpenGLRenderer: HWUI protection enabled for context ,  &this =0xaef22088 ,&mEglDisplay = 1 , &mEglConfig = 8 
01-23 20:19:54.962 31538-31583/dev.kdeveloper.ballrun D/OpenGLRenderer: Enabling debug mode 0
01-23 20:19:55.122 31538-31538/dev.kdeveloper.ballrun I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2bd9790c time:36886929
01-23 20:19:55.932 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:19:56.352 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:19:57.712 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:01.892 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:02.412 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:02.842 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:03.342 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:03.652 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:03.952 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:04.232 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:04.672 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:06.162 31538-31538/dev.kdeveloper.ballrun D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-23 20:20:28.892 31538-31544/dev.kdeveloper.ballrun I/art: Thread[2,tid=31544,WaitingInMainSignalCatcherLoop,Thread*=0xaf60e400,peer=0x12c8b080,"Signal Catcher"]: reacting to signal 3
01-23 20:20:29.342 31538-31544/dev.kdeveloper.ballrun I/art: Wrote stack traces to '/data/anr/traces.txt'
01-23 20:23:28.322 31538-31545/dev.kdeveloper.ballrun W/art: Suspending all threads took: 5.108ms
01-23 20:26:47.482 31538-31545/dev.kdeveloper.ballrun W/art: Suspending all threads took: 7.433ms

When i run the application on my samsung galaxy s5, of which has enough ram and memory, the program freezes and an error comes up saying that the app has stopped working.

Any ideas?


Solution

  • Never - never - never do

    1)

     try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    

    2) make a while (true) loop

    3) taking any long/heavy operation (f.e. network calls)

    in UI Thread.

    This thread (also called Main Thread) has a responsibility to update application interface. The system makes a regular check if the thread can response. So if the thread cannot answer (because of the reasons i mention above), the ANR (Application not responding) exception is raised.