2016-01-23 40 views
0

在這個應用程序,我試圖讓一個小的Button橫跨屏幕移動,並讓用戶嘗試並避免屏幕上緩慢下降的塊。應用程序凍結,Android工作室錯誤?

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: [email protected] 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 

當我運行的應用程序我三星Galaxy S5,其中具有足夠的RAM和內存,程序凍結和錯誤出現說該應用程序已停止加工。

任何想法?

+1

當應用程序凍結時,您可以顯示log cat的輸出嗎? –

+0

我已更新它,包括logcat –

+0

這是錯誤日誌嗎?我認爲這不是應用程序凍結的地方。 –

回答

1

從不 - 永遠 - 永遠不會做

1)

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

2)使while (true)

3)採取任何長/重操作(FE網絡電話)

在UI線程中。

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

+0

請您解釋一下嗎?或添加更多信息?也許OP不知道這些東西哈?對?有人告訴我不要那樣做之前,我不完全明白爲什麼,我不知道設置主線程睡眠1000毫秒 – Mohsen

+0

,它仍然想出了同樣的問題,所以我不相信這是造成應用程序凍結,但感謝您試用 –

+0

此外,我試圖移除任何方式感謝您 –

相關問題