簡體   English   中英

應用程序保持凍結,Android Studio錯誤?

[英]App keeps freezing,Android studio error?

在這個應用程序中,我試圖獲得一個小的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

當我在具有足夠內存和內存的三星s5上運行該應用程序時,程序凍結,出現錯誤並指出該應用程序已停止工作。

有任何想法嗎?

永不-永不-永不做

1)

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

2)做while (true)循環

3)進行長時間/繁重的操作(例如網絡通話)

在UI線程中。

該線程(也稱為主線程)負責更新應用程序接口。 系統會定期檢查線程是否可以響應。 因此,如果線程無法回答(由於我上面提到的原因),則會引發ANR(應用程序無響應)異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM