A wait/sleep in a while loop?

Put your problem here if it does not fit any of the other categories.

A wait/sleep in a while loop?

Postby Kalder » Sun Jan 22, 2012 6:21 am

Hello all,
I'm at the very beginning of learning android development.
At the moment I'm trying to play with the views, I managed to get a copy/pasted code working, but it was not how i wanted it.
To make it simple, I just want a view switching thing just like the home screen.
One that doesn't wait for your gesture to be made before it switches to another layout (that's what i copied from some sample code...)

Now I think I made a step towards a solutions, but the wait in the while loop needs a try/catch thing.

This is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.kalder.news;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.MotionEvent;
  7. import android.widget.ViewFlipper;
  8.  
  9. public class StartingActivity extends Activity {
  10.  
  11.         /** Called when the activity is first created. */
  12.  
  13.         public ViewFlipper vf;
  14.         private float oldTouchValue;
  15.         private float returner;
  16.  
  17.         @Override
  18.         public void onCreate(Bundle savedInstanceState) {
  19.                 super.onCreate(savedInstanceState);
  20.                 setContentView(R.layout.main);
  21.  
  22.                 vf = (ViewFlipper) findViewById(R.id.layoutswitcher);
  23.  
  24.         }
  25.  
  26.         @Override
  27.         public boolean onTouchEvent(MotionEvent touchEvent) {
  28.                 switch (touchEvent.getAction()) {
  29.  
  30.                 case MotionEvent.ACTION_DOWN: {
  31.                         oldTouchValue = touchEvent.getX();
  32.                         break;
  33.                 }
  34.  
  35.                 case MotionEvent.ACTION_MOVE: {
  36.                         // TODO: Some code to make the ViewFlipper
  37.                         // act like the home screen.
  38.                         vf.scrollTo((int) (touchEvent.getX() * -1) + (int) oldTouchValue,
  39.                                         vf.getScrollY());
  40.                         returner = (int) (touchEvent.getX() + oldTouchValue);
  41.                         break;
  42.                 }
  43.  
  44.                 case MotionEvent.ACTION_UP: {
  45.                         int waiter = 0;
  46.                         while (waiter != 100) {
  47.                                 vf.scrollTo((int) returner, (int) vf.getScrollY());
  48.                                 returner = (int) (returner / 1.1);
  49.                                 try {
  50.                                         wait(100);
  51.                                 } catch (InterruptedException e) {
  52.                                         e.printStackTrace();
  53.                                 }
  54.                                 waiter++;
  55.                                 Log.d("check out", "returner= " + returner);
  56.                         }
  57.                 }
  58.  
  59.                 }
  60.                 return true;
  61.         }
  62. }
  63.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


So, I wanted the view to smoothly return back to the original X position and made the code in the "case MotionEvent.ACTION_UP:"-part for that.
There are most probably better ways to make this work, but I didn't want to skip the beginner levels of coding...
It's probably far from done, but I can't manage to test what happens, since when i run the app without the try{wait}catch{etc.. it places the view back instantly, but when i added the wait(100) it gave me an error telling me I had to surround it with the try / catch things, so I just hovered over the problem and implemented the try and catch things, now however the app force closes when i release the mouse button after scrolling the view.

I think this is the important line from the LogCat:
E/AndroidRuntime(330): java.lang.IllegalMonitorStateException: object not locked by thread before wait()


here is the rest which is like chinese to me:
Syntax: [ Download ] [ Hide ]
  1. 01-22 05:25:53.652: E/AndroidRuntime(330): java.lang.IllegalMonitorStateException: object not locked by thread before wait() 
  2. 01-22 05:25:53.652: E/AndroidRuntime(330):     at java.lang.Object.wait(Native Method) 
  3. 01-22 05:25:53.652: E/AndroidRuntime(330):     at java.lang.Object.wait(Object.java:326) 
  4. 01-22 05:25:53.652: E/AndroidRuntime(330):     at com.kalder.news.StartingActivity.onTouchEvent(StartingActivity.java:56) 
  5. 01-22 05:25:53.652: E/AndroidRuntime(330):     at android.app.Activity.dispatchTouchEvent(Activity.java:2089) 
  6. 01-22 05:25:53.652: E/AndroidRuntime(330):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655) 
  7. 01-22 05:25:53.652: E/AndroidRuntime(330):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785) 
  8. 01-22 05:25:53.652: E/AndroidRuntime(330):     at android.os.Handler.dispatchMessage(Handler.java:99) 
  9. 01-22 05:25:53.652: E/AndroidRuntime(330):     at android.os.Looper.loop(Looper.java:123) 
  10. 01-22 05:25:53.652: E/AndroidRuntime(330):     at android.app.ActivityThread.main(ActivityThread.java:4627) 
  11. 01-22 05:25:53.652: E/AndroidRuntime(330):     at java.lang.reflect.Method.invokeNative(Native Method) 
  12. 01-22 05:25:53.652: E/AndroidRuntime(330):     at java.lang.reflect.Method.invoke(Method.java:521) 
  13. 01-22 05:25:53.652: E/AndroidRuntime(330):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
  14. 01-22 05:25:53.652: E/AndroidRuntime(330):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
  15. 01-22 05:25:53.652: E/AndroidRuntime(330):     at dalvik.system.NativeStart.main(Native Method) 


So what does it mean, "Object not locked", I couldn't figure it out, so hopefully someone here can tell me what to do.
References to other solutions will also be helpful.

Help would be greatly appreciated.
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Top

Re: A wait/sleep in a while loop?

Postby Kalder » Sun Jan 22, 2012 9:31 am

Ok the sleep problem got fixed...
Now it just needs to MOVE!
Y U NO MOVE?!

More simplistic example then this I couldn't make... but in theory this is what i'm trying to do, but it won't work.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. value = 100;
  2. while ( value != 0 ) {
  3.     value--
  4.     moveSomethingToX.scrollTo(value, 0);
  5.     try {
  6.         thread.sleep(100);
  7.     } catch (Exception e) {
  8.     }
  9. }
  10.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

can .scrollTo(x, y) actually be used in a loop? cause it seems to ignore it all the time.
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Re: A wait/sleep in a while loop?

Postby Phyll » Sun Jan 22, 2012 7:04 pm

Hi Kalder,

You may have your expectations set too high for ViewFlipper.

How's come you're not using the showNext() method to bring the next view into view?

If you'd like to send all your code up. I'll take it a look at it. It's hard to tell what's going on without the xml? Do you have a ScrollView in there too?

Help us help you.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: A wait/sleep in a while loop?

Postby Kalder » Mon Jan 23, 2012 4:29 pm

I've deleted the project and began working on parsing RSS, anyway about this topic, I had the showNext() and showPrevious() set up on a simple gesture (taking x position when pressed and released), and that worked pretty fluid, but I always want to improve what i have, so I wanted to make the view follow my finger until i released and then showNext from that position but I guess it was not the right approach.
My Idea was to have a viewflipper like the homescreen.

Anyway thanks for your reply, I'll have to give it another shot sooner or later ;)
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Re: A wait/sleep in a while loop?

Postby Phyll » Mon Jan 23, 2012 5:01 pm

Hi Kalder,

I think the viewpager does what you want.
It requires version 10 and the compatibilty library or later but it does that.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests