Question

I would like to make an android app that will complete its start up and then run a while loop until the user had completed a certain task. Then the code would run another function to make sure the user has completed the task correctly. How should I be going about this. I have tried putting the while loop in onCreate, onStart, and onResume and in all the while loop runs forever without letting the GUI get drawn, therefore making the task impossible to complete (Also sometimes I get a ANR message). Where should I put the while loop so that it runs right after the start of that activity and doesn't interfere with the startup?

Thanks,

andrewgies17

EDIT 3-31-13 2:45 PST:

So I took RyPope's advice and created a new thread, and am calling a function from the main thread and it doesn't like that. Here is all my code:

` public class MainActivity extends Activity {

public EditText pass1, pass2, pass3, pass4;
public int current = 1;
public boolean enteringCode = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pass1 = (EditText) findViewById(R.id.pass1);
    pass2 = (EditText) findViewById(R.id.pass2);
    pass3 = (EditText) findViewById(R.id.pass3);
    pass4 = (EditText) findViewById(R.id.pass4);

    new Thread(new Runnable() {
        public void run() {
            checkInputs();
        }
    }).start();
}

public void checkInputs() {
    while(enteringCode) {
        if(pass1.getText().toString().length() > 0) {
            current = 2;
        }
        if(pass2.getText().toString().length() > 0) {
            current = 3;
        }
        if(pass3.getText().toString().length() > 0) {
            current = 4;
        }
        if(pass4.getText().toString().length() > 0) {
            enteringCode = false;
            System.out.println(Integer.parseInt(pass1.getText().toString() + pass2.getText().toString() + pass3.getText().toString() + pass4.getText().toString()));
            authenticateCode(Integer.parseInt(pass1.getText().toString() + pass2.getText().toString() + pass3.getText().toString() + pass4.getText().toString()));
        }
        if(current == 1) {
            pass1.requestFocus();
        }
        if(current == 2) {
            pass2.requestFocus();
        }
        if(current == 3) {
            pass3.requestFocus();
        }
        if(current == 4) {
            pass4.requestFocus();
        }
    }
}

public void authenticateCode(int code) {

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} ` So when this runs and the view tries to request focus it crashes and I get the following logcat output:

03-31 14:46:54.803: E/AndroidRuntime(10922): FATAL EXCEPTION: Thread-4197 03-31 14:46:54.803: E/AndroidRuntime(10922): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4062) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:730) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:779) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.View.invalidate(View.java:8605) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.View.onFocusChanged(View.java:3884) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.widget.TextView.onFocusChanged(TextView.java:8281) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.View.unFocus(View.java:3814) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.ViewGroup.requestChildFocus(ViewGroup.java:530) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.View.handleFocusGainInternal(View.java:3706) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.View.requestFocus(View.java:5402) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.View.requestFocus(View.java:5352) 03-31 14:46:54.803: E/AndroidRuntime(10922): at android.view.View.requestFocus(View.java:5330) 03-31 14:46:54.803: E/AndroidRuntime(10922): at com.thirteenbit.driveSafe.MainActivity.checkInputs(MainActivity.java:51) 03-31 14:46:54.803: E/AndroidRuntime(10922): at com.thirteenbit.driveSafe.MainActivity$1.run(MainActivity.java:26) 03-31 14:46:54.803: E/AndroidRuntime(10922): at java.lang.Thread.run(Thread.java:856)

Anybody got a solution to this predicament?

Was it helpful?

Solution

Probably best to create a new thread so that the start Up doesn't get hung up on the loop. Try this in the onCreate:

new Thread(new Runnable() {
    public void run() {
        while(true)
        {

        }
    }
}).start();

OTHER TIPS

I think your concept is flawed. You probably don't really want a loop to be running. You want something to happen after the user has completed a task. You should use a listener that activates after your user has completed their task.

When you want to make some code run in loop you have to run it on new thread, because when do this on main(UI) thread then your application can't update UI because main thread is used by you all the time. So try to make new thread or timer(it is better).

I hope that this was heplful for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top