2015-09-25 17 views
0

我正在進行此初始屏幕測試,並實施了一個點擊監聽器,以便可以通過點擊屏幕跳過它。問題在於意圖被調用兩次,因爲加載線程在點擊屏幕時未正確中斷。我做錯了什麼,以及如何避免這個錯誤?如何使用onClickListener禁用線程?

public class SplashScreen extends Activity { 
    final Thread Loading = new Thread(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(R.layout.splashscreen); 
    ImageView SplashScreenView = (ImageView) findViewById(R.id.SplashScreenAnimation); 
    SplashScreenView.setBackgroundResource(R.drawable.flashscreenanimation); 
    AnimationDrawable SplashScreenAnimation = (AnimationDrawable) SplashScreenView.getBackground(); 
    SplashScreenAnimation.start(); 
    RelativeLayout OnTouchSkipScreen = (RelativeLayout)findViewById(R.id.SplashScreenView); 
    OnTouchSkipScreen.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Loading.interrupt(); 
      Loading.isInterrupted(); 
      Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class); 
      startActivity(SplashScreen); 
      overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
      finish(); 
     } 
    }); 
    Thread Loading = new Thread() { 
     public void run() { 
      try { 
       sleep(2573); 
       Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class); 
       startActivity(SplashScreen); 
       overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       finish(); 
      } 
     } 
    }; 
    Loading.start(); 
} 
} 
+0

爲什麼有兩個加載線程在你的代碼? – Kunu

+0

你是什麼意思? –

+0

如果你檢查你的代碼,你可以看到你聲明瞭'final Thread Loading = new Thread();'兩次。 – Kunu

回答

1

去了

public class SplashScreen extends Activity { 
    Thread Loading ; 
    boolean flag = true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     setContentView(R.layout.splashscreen); 
     ImageView SplashScreenView = (ImageView) findViewById(R.id.SplashScreenAnimation); 
     SplashScreenView.setBackgroundResource(R.drawable.bontactbook); 
     AnimationDrawable SplashScreenAnimation = (AnimationDrawable) SplashScreenView.getBackground(); 
     SplashScreenAnimation.start(); 
     RelativeLayout OnTouchSkipScreen = (RelativeLayout) findViewById(R.id.flashscreenanimation); 
     OnTouchSkipScreen.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       flag = false; 
       Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class); 
       startActivity(SplashScreen); 
       finish(); 
      } 
     }); 
     Thread Loading = new Thread() { 
      public void run() { 
       try { 
        sleep(5000); 
        if (flag) { 
         Intent SplashScreen = new Intent(SplashScreen.this, HomeScreen.class); 
         startActivity(SplashScreen); 
         overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        finish(); 
       } 
      } 
     }; 
     Loading.start(); 
    } 
} 
+0

非常感謝你,作品像一個魅力! –