android中的while循环不工作 package veda.software.game; 导入java.util.ArrayList; 导入java.util.Iterator; 导入android.app.Activity; 导入android.app.AlertDialog; 导入android.content.Context; 导入android.content.Intent; 导入android.os.Bundle; 导入android.os.Handler; 导入android.util.Log; 导入android.view.Gravity; 导入android.view.view; 导入android.widget.Button; 导入android.widget.TextView; 导入android.widget.Toast; 公共课的随机性可扩展活动{ 视图v1=新视图(getApplicationContext()); TextView-txtName; 按钮提交按钮; 智力选择; 整数级=1; 字符串toRem=“”; int-life=0; 智力得分; 布尔值watsUp1=true; Handler myHandler=newhandler(); int noOfGames=0; 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.screen2); txtName=(TextView)findViewById(R.id.editText1); submitbutton=(按钮)findViewById(R.id.button1); Intent i=getIntent(); choice=Integer.parseInt(i.getStringExtra(“choice”); 这就是生活; } 公共生活{ while(true){ //TODO自动生成的方法存根 如果(watsUp1==真){ Context=getApplicationContext(); CharSequence text=“您必须记住!!“+”和“+level.toString()+”从上一个开始”; int duration=Toast.LENGTH\u SHORT; Toast Toast=Toast.makeText(上下文、文本、持续时间); toast.setGravity(Gravity.TOP | Gravity.LEFT,0400); toast.show(); watsUp1=假; noOfGames++; 如果(noOfGames==5){ noOfGames=0; 级别++; } 如果(级别==9){ 打破 } 否则{ RandomNumGativity.this.startTheGame(); } } } } public int getArraySize(){ int arraySize=0+(int)(Math.random()*((9-0)+1)); 返回阵列化; } 公开作废开始游戏(){ ArrayList arrayOfNum=新的ArrayList(); int arraySize=this.getArraySize(); 如果(arraySize

android中的while循环不工作 package veda.software.game; 导入java.util.ArrayList; 导入java.util.Iterator; 导入android.app.Activity; 导入android.app.AlertDialog; 导入android.content.Context; 导入android.content.Intent; 导入android.os.Bundle; 导入android.os.Handler; 导入android.util.Log; 导入android.view.Gravity; 导入android.view.view; 导入android.widget.Button; 导入android.widget.TextView; 导入android.widget.Toast; 公共课的随机性可扩展活动{ 视图v1=新视图(getApplicationContext()); TextView-txtName; 按钮提交按钮; 智力选择; 整数级=1; 字符串toRem=“”; int-life=0; 智力得分; 布尔值watsUp1=true; Handler myHandler=newhandler(); int noOfGames=0; 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.screen2); txtName=(TextView)findViewById(R.id.editText1); submitbutton=(按钮)findViewById(R.id.button1); Intent i=getIntent(); choice=Integer.parseInt(i.getStringExtra(“choice”); 这就是生活; } 公共生活{ while(true){ //TODO自动生成的方法存根 如果(watsUp1==真){ Context=getApplicationContext(); CharSequence text=“您必须记住!!“+”和“+level.toString()+”从上一个开始”; int duration=Toast.LENGTH\u SHORT; Toast Toast=Toast.makeText(上下文、文本、持续时间); toast.setGravity(Gravity.TOP | Gravity.LEFT,0400); toast.show(); watsUp1=假; noOfGames++; 如果(noOfGames==5){ noOfGames=0; 级别++; } 如果(级别==9){ 打破 } 否则{ RandomNumGativity.this.startTheGame(); } } } } public int getArraySize(){ int arraySize=0+(int)(Math.random()*((9-0)+1)); 返回阵列化; } 公开作废开始游戏(){ ArrayList arrayOfNum=新的ArrayList(); int arraySize=this.getArraySize(); 如果(arraySize,android,while-loop,android-activity,Android,While Loop,Android Activity,你在那里有一些非常糟糕的事情 造成黑屏的原因是无休止的while(真)loop,其中设置了watsUp1=false因此watsUp1==true永远都不是真的,它永远循环,不执行任何操作。使用无限循环的事实可能是问题所在。事实上,我想不出有多少情况下无限循环不是问题。 无论如何,您的循环似乎在循环的第一个if块中迭代一次 如果(watsUp1==真){ 然后在那个块中设置watsUp1=false,这不会是一个问题,除非你在无限循环中 此外,我还将致力于使您的代码在将来更美观。我现在不想阅读那

你在那里有一些非常糟糕的事情


造成黑屏的原因是无休止的
while(真)
loop,其中设置了
watsUp1=false
因此
watsUp1==true
永远都不是真的,它永远循环,不执行任何操作。

使用无限循环的事实可能是问题所在。事实上,我想不出有多少情况下无限循环不是问题。 无论如何,您的循环似乎在循环的第一个if块中迭代一次

如果(watsUp1==真){

然后在那个块中设置watsUp1=false,这不会是一个问题,除非你在无限循环中


此外,我还将致力于使您的代码在将来更美观。

我现在不想阅读那么多代码。但是,我非常愿意提供帮助。首先,我建议您制作一个尽可能简单的程序,以说明您正在尝试做什么。如果您在这方面有困难,请返回d编辑你的问题,包括你尝试过的细节。阐述代码大师的答案——“它不起作用”是不够的,它应该做什么,它在做什么,日志中有什么信息,并将其缩减到最小(不起作用)的示例是必要的。
package veda.software.game;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class RandomNumGenActivity extends Activity  {
    View v1 = new View(getApplicationContext());
    TextView txtName;
    Button submitbutton;
    int choice;
    Integer level = 1;
    String toRem = "";
    int life = 0;
    int score;
    boolean watsUp1 = true;
    Handler myHandler =  new Handler();
    int noOfGames = 0;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        txtName = (TextView) findViewById(R.id.editText1);
        submitbutton = (Button) findViewById(R.id.button1);
        Intent i = getIntent();
        choice = Integer.parseInt(i.getStringExtra("choice"));
        this.scoreLife(); 
    }
    public void scoreLife(){
        while(true){
            // TODO Auto-generated method stub
            if(watsUp1==true){
                Context context = getApplicationContext();
                CharSequence text = "you have to remember the !!"+"The"+level.toString()+"from last";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 400);
                toast.show();
                watsUp1=false;
                noOfGames++;
                if(noOfGames==5){
                    noOfGames=0;
                    level++;
                }
                if(level==9){
                    break;
                }
                else{
                    RandomNumGenActivity.this.startTheGame();
                }
            }
        }
    }
    public int getArraySize(){
        int arraySize = 0 + (int)(Math.random() * ((9 - 0) + 1));
        return arraySize;
    }
    public void startTheGame(){
        ArrayList<Integer> arrayOfNum = new ArrayList<Integer>();
        int arraySize = this.getArraySize();
        if(arraySize<level&&arraySize<4){
            arraySize = this.getArraySize();
        }
        else{
            for (int idx = 1; idx <= arraySize; ++idx){
                int randomInt = choice*1 + (int)(Math.random() * ((choice*9 - choice*1) + 1));
                arrayOfNum.add(randomInt);
                Log.d("randomInt are:",""+randomInt);
            }
            this.theGame(arrayOfNum);
        }
    }
    public void theGame(ArrayList<Integer> theArray){
        Iterator<Integer> ite = theArray.iterator();
        int counter = 0;
        while(ite.hasNext()){
            counter++;
            String temp = ite.next().toString();
            if(counter == theArray.size()-level){
                toRem = temp.toString();
            }

            Log.d("random array elements are:",""+temp);
            showToast(temp);
        }
    }   
    public void showToast(String txtToDisplay){
        Log.d("inside showToast",""+txtToDisplay);
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, txtToDisplay, duration);
        toast.show();
    }
    public void buttonClick(View v1){
        final Handler handler = new Handler();
        final Runnable updater = new Runnable() {
            public void run() {
                String req = txtName.getText().toString();
                System.out.println("req is"+req );
                if(req.equals(toRem)){
                    Context context = getApplicationContext();
                    CharSequence text = "yo!! you got it right buddy";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    watsUp1=true;
                }
                else{
                    Context context = getApplicationContext();
                    CharSequence text = "you are fired!!";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    watsUp1=true;
                }
            }
        };
        Thread x = new Thread() {
            public void run() {
                handler.post(updater);
            }
        };
    }   
}