应用程序一直冻结,Android工作室错误?

时间:2016-01-23 19:49:45

标签: java android xml android-studio gestures

在这个应用程序中,我试图让一个小的Button移动到屏幕上,并让用户尝试避免缓慢掉落在屏幕上的块。

XML布局:

<?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类:

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);
    }

}

动画布局:

<?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>

其余大部分文件都很相似。这些文件为点​​之间的过渡设置动画。按钮将在三个不同区域之间移动。这意味着动画并不是那么重要,因为它们起作用,当我点击文本视图时应用程序会冻结。这也是在调用主循环时。这可能是问题吗?

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

当我在我的 samsung galaxy s5 上运行应用程序时,其中有足够的内存和内存,程序会冻结并出现错误,说应用程序已停止工作。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

从不 - 从不 - 永远不会

1)

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

2)制作while (true)循环

3)进行任何长/重操作(例如网络呼叫)

UI线程中的

此线程(也称为主线程)负责更新应用程序界面。系统会定期检查线程是否可以响应。因此,如果线程无法回答(由于我上面提到的原因),则会引发ANR(应用程序未响应)异常。