-2

I'm new to Android Studio programming and I'd like to know what is the substitute for code below.. I'm trying to iterate an infinite loop that has a nested one and it seem to not work. The application still crushes when it comes to that loop.

I also tried to use non-infinite loop without nesting another inside of it.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    while (state) {
        int i = 0;

        while (i < person[i].length) {
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    someTextView.setText(person[i].getName());
                }
            });
        }
        i++;
    }
}

This Logcat below just represents the nested while loop without the onClickListener unlike shown in code.

2019-06-01 11:59:10.695 19529-19529/com.example.app2 I/Timeline: Timeline: Activity_launch_request id:com.example.app2 time:120814793
2019-06-01 11:59:10.771 19529-19543/com.example.app2 I/art: Enter while loop.
2019-06-01 11:59:10.789 19529-19543/com.example.app2 I/art: Enter while loop.

After entering while loop all I'm getting is a black screen on my device.

How do I use these loops within onCreate() method?

Mato
  • 11
  • 1
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Jun 01 '19 at 10:08
  • Why you are adding `setOnClickListener()` inside loop?? – AskNilesh Jun 01 '19 at 10:11
  • I might be wrong, but the real problem here is, that the loop doesn't work at all – Mato Jun 01 '19 at 10:15
  • This is an XY problem. Please describe your end goal, not how you plan to achieve it. Infinite loops are rarely required in common Android programming. – charles-allen Jun 01 '19 at 10:38
  • I want an infinite iterations of card displaying game - where the name of the player is shown when on turn. At one point I'm planning to decide to change the value of infinite loop parameter to false and end the game. – Mato Jun 01 '19 at 10:46
  • What are you trying to do? – Shaon Jun 01 '19 at 10:46
  • Your turns should either be triggered by time (postDelayed) or by user actions (onClick). You don't need a loop. Each time the function is called, increment your counter. TL;DR: either the user or the clock controls your turns, not a CPU loop. – charles-allen Jun 01 '19 at 10:57

2 Answers2

1

I have a few questions. If you want a infinite loop why not just:

while(true)
{
    //Execute some code
}

My next question is why are you trying to do this? The reason why you are getting a black screen is cause you are stuck in an infinite loop. The code for the nested loop gets executed but because you are incrementing i outside the nested loop you will always return true, i will always be less than the length of person[]. There is nothing to render because a result is never reached. If you are looking to add onClickListeners to multiple objects the far better approach is to use a Recycler view with cards, and to assign the OnclickListeners in the Recycler adapter.

  • I know, but I'm planning to change the "state" to false by another button in a few minutes, and it doesn't work either.. But I appreciate your comment and I'l try your suggestion. Thank you! – Mato Jun 01 '19 at 10:36
  • How will you change the state to false if you never leave the nested loop? – Alexander Robinson Jun 01 '19 at 10:47
  • by another onClickListener where the "sate = false;" will take place. I've tried this, but it doesn't work, som i'm about to learn more about the Recycler view/adapter you mentioned.. – Mato Jun 01 '19 at 10:50
  • How do you leave the nested loop when it always evaluates true. A while loop will execute it's code block till it evaluates to false. Since you increment i OUTSIDE the nested loop, i will always be zero and will always be less than the length of person[]. – Alexander Robinson Jun 01 '19 at 10:57
  • @mato - This is not how control flow works on Android. You should not block the UI thread with a loop. Instead schedule an event to occur later (with postDelayed or a UI event handler e.g. onclick). – charles-allen Jun 01 '19 at 11:05
0

Assuming your goal is to do something repeatedly, e.g. every 1 second, you could use postDelayed(...)

// Java
Handler handler = new Handler();
int delay = 1000; // milliseconds

handler.postDelayed(new Runnable() {
    public void run() {
        // do something
        handler.postDelayed(this, delay);
    }
}, delay);

Source: How to run a method every X seconds

charles-allen
  • 3,891
  • 2
  • 23
  • 35