我已经设法让它在按下对话框后运行到点,它运行主UI,但最终在我移动到另一个EditText后崩溃了。我不知道如何使用onProgressUpdate()来确保相应地禁用我的EditText。
我正在寻找的是,如果用户点击大字符,我希望程序使用While循环检查ToggleButton条件。如果它仍然打开,我希望它基本上检查EditText焦点并有一些SetOnFocusChangeListener();它给我一个错误说Only the original thread that created a view hierarchy can touch its views.
这是我在onCreate()中的主要类:
//Row 1-4 EditText
row1TextField = (EditText) findViewById(R.id.row1);
row2TextField = (EditText) findViewById(R.id.row2);
row3TextField = (EditText) findViewById(R.id.row3);
row4TextField = (EditText) findViewById(R.id.row4);
//ToggleButton
toggleButton = (ToggleButton) findViewById(R.id.toggle1);
AlertDialog.Builder characterChoose = new AlertDialog.Builder(context);
characterChoose.setTitle("Character Input Option");
characterChoose.setMessage("Please choose the option the below").setCancelable(false)
.setPositiveButton("Large Character", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toggleButton.setTextOn("LARGE");
toggleButton.setChecked(true);
backgroundThread bckgnd = new backgroundThread();
bckgnd.execute();
}
这是我的AsyncTask类:
private class backgroundThread extends AsyncTask<Boolean, Boolean, Boolean>{
//Run on background thread.
@Override
protected Boolean doInBackground(Boolean... params) {
while(toggleButton.isChecked())
{
if(row1TextField.isFocused())
{
row2TextField.setEnabled(false);
}
row2TextField.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View textfield3, boolean isFocus) {
if(textfield3.isFocused())
{
row3TextField.setEnabled(false);
}
}
});
row3TextField.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View textfield3, boolean isFocus) {
if(textfield3.isFocused())
{
row4TextField.setEnabled(false);
}
}
});
}
return null;
}
logcat的
01-03 07:54:17.179: E/AndroidRuntime(4871): FATAL EXCEPTION: AsyncTask #1
01-03 07:54:17.179: E/AndroidRuntime(4871): java.lang.RuntimeException: An error occured while executing doInBackground()
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.lang.Thread.run(Thread.java:856)
01-03 07:54:17.179: E/AndroidRuntime(4871): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4888)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:975)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4125)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.view.View.invalidate(View.java:10521)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.widget.TextView.invalidateDrawable(TextView.java:4849)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.graphics.drawable.DrawableContainer.invalidateDrawable(DrawableContainer.java:250)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:382)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.graphics.drawable.Drawable.setVisible(Drawable.java:578)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:307)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:106)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.graphics.drawable.Drawable.setState(Drawable.java:494)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.view.View.drawableStateChanged(View.java:14372)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.widget.TextView.drawableStateChanged(TextView.java:3381)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.view.View.refreshDrawableState(View.java:14386)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.view.View.setEnabled(View.java:5733)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.widget.TextView.setEnabled(TextView.java:1402)
01-03 07:54:17.179: E/AndroidRuntime(4871): at com.example.dotmatrixledpanel.startPage2$backgroundThread.doInBackground(startPage2.java:313)
01-03 07:54:17.179: E/AndroidRuntime(4871): at com.example.dotmatrixledpanel.startPage2$backgroundThread.doInBackground(startPage2.java:1)
01-03 07:54:17.179: E/AndroidRuntime(4871): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-03 07:54:17.179: E/AndroidRuntime(4871): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-03 07:54:17.179: E/AndroidRuntime(4871): ... 5 more
所以我想知道,我应该在onProgressUpdate方法中添加什么,还是应该使用Runnable?
答案 0 :(得分:0)
您无法在异步任务的doInBackground()方法中对UI进行更改。为此,您必须使用异步任务的onPostExecute()方法。所以你的doInBackground()应该返回一个布尔值或你想要的东西,这样你就可以在onPostExecute()方法中改变UI。
答案 1 :(得分:0)
不允许来自doInBackground()
的UI触摸。首先检查AsyncTask文档:http://developer.android.com/reference/android/os/AsyncTask.html
异步任务由在a上运行的计算定义 后台线程,其结果发布在UI线程上。
如果您需要在UI端更新任何内容,则需要在onPreExecute()
或onPostExecute()
中执行此操作。
修改强>
我没有看到任何理由在您的代码中使用AsyncTask
。