我想制作一个 android 应用程序,它将完成它的启动,然后运行一个 while 循环,直到用户完成某个任务。然后代码将运行另一个函数以确保用户已正确完成任务。我该怎么办。我尝试将 while 循环放在 onCreate、onStart 和 onResume 中,并且在所有 while 循环中永远运行而不让 GUI 被绘制,因此使任务无法完成(有时我也会收到 ANR 消息)。我应该将 while 循环放在哪里,以便它在该活动开始后立即运行并且不会干扰启动?
谢谢,
安德鲁吉斯17
编辑 3-31-13 2:45 PST:
所以我接受了 RyPope 的建议并创建了一个新线程,并从主线程调用了一个函数,但它不喜欢那样。这是我的所有代码:
` 公共类 MainActivity 扩展 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;
}
} ` 所以当它运行并且视图尝试请求焦点时它崩溃并且我得到以下 logcat 输出:
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)
有人有解决这个困境的办法吗?