Changing image in imageview using Threads

前端 未结 6 1032
失恋的感觉
失恋的感觉 2020-12-11 10:05

I\'m getting error with this code. Why huhu 123123123

Thread timer = new Thread()
{
    public void run()
    {
        try
        {
            sleep(1500)         


        
6条回答
  •  一个人的身影
    2020-12-11 11:02

    You should update ui on the ui thread. Use runonUithread.

      runOnUiThread(new Runnable() {
    
      @Override
      public void run() {
       // set image to imageview here
       // ui should be updated on the ui thread.
       // you cannot update ui from a background thread  
      }
     });
    

    But i would suggest you to use a handler.

    public class Splash extends Activity {
    
    //stopping splash screen starting home activity.
    private static final int STOPSPLASH = 0;
    //time duration in millisecond for which your splash screen should visible to
      //user. here i have taken half second
    private static final long SPLASHTIME = 500;
    
    //handler for splash screen
    private Handler splashHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
              switch (msg.what) {
                case STOPSPLASH:
                    //Generating and Starting new intent on splash time out 
                    Intent intent = new Intent(Splash.this, 
                                             MainActivity.class);
                    startActivity(intent);
                        Splash.this.finish(); 
                    break;
              }
              super.handleMessage(msg);
         }
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    
        //Generating message and sending it to splash handle 
        Message msg = new Message();
        msg.what = STOPSPLASH;
        splashHandler.sendMessageDelayed(msg, SPLASHTIME);
    }
    }
    

    splash.xml

    
    
         // have a imageview and set background to imageview   
    
    
    

    Using handlers and postdelayed

    public class Splash extends Activity {
    private static final int SPLASH_TIME = 2 * 1000;// 3 seconds
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        ImageView iv= (ImageView) findViewById(R.id.imageView1);
        iv.setBackgroundResource(R.drawable.afor);
        try {
        new Handler().postDelayed(new Runnable() {
    
            public void run() {
    
                Intent intent = new Intent(Splash.this,
                    MainActivity.class);
                startActivity(intent);
    
                Splash.this.finish();
            }
    
    
        }, SPLASH_TIME);
    
        } catch(Exception e)
           {
            e.printStacktrace();
           }
    }
    
    
    @Override
    public void onBackPressed() {
        this.finish();
        super.onBackPressed();
    }
    }
    

    splash.xml

    
    
    
    
    
    
    

提交回复
热议问题