/** * Creates a new asynchronous task. This constructor must be invoked on the UI thread. */ public AsyncTask() { mWorker = new WorkerRunnable<Params, Result>() { public Result call() throws Exception { mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); //noinspection unchecked Result result = doInBackground(mParams); Binder.flushPendingCommands(); return postResult(result); } };
Process: com.example.aaron.helloworld, PID: 659 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6981) at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1034) at android.view.View.requestLayout(View.java:17704) at android.view.View.requestLayout(View.java:17704) at android.view.View.requestLayout(View.java:17704) at android.view.View.requestLayout(View.java:17704) at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:380) at android.view.View.requestLayout(View.java:17704) at android.widget.TextView.checkForRelayout(TextView.java:7109) at android.widget.TextView.setText(TextView.java:4082) at android.widget.TextView.setText(TextView.java:3940) at android.widget.TextView.setText(TextView.java:3915) at com.example.aaron.helloworld.MainActivity$MAsyncTask.onPreExecute(MainActivity.java:53) at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) at android.os.AsyncTask.execute(AsyncTask.java:535) at com.example.aaron.helloworld.MainActivity$1$1.run(MainActivity.java:40) at java.lang.Thread.run(Thread.java:818)
@MainThread public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, Params... params) { if (mStatus != Status.PENDING) { switch (mStatus) { case RUNNING: throw new IllegalStateException("Cannot execute task:" + " the task is already running."); case FINISHED: throw new IllegalStateException("Cannot execute task:" + " the task has already been executed " + "(a task can be executed only once)"); } }
final MAsyncTask mAsyncTask = new MAsyncTask(); title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /*MLog.e("you have clicked the title textview!!!"); Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, 101);*/
private static class InternalHandler extends Handler { public InternalHandler() { super(Looper.getMainLooper()); }
@SuppressWarnings({"unchecked", "RawUseOfParameterizedType"}) @Override public void handleMessage(Message msg) { AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj; switch (msg.what) { case MESSAGE_POST_RESULT: // There is only one result result.mTask.finish(result.mData[0]); break; case MESSAGE_POST_PROGRESS: result.mTask.onProgressUpdate(result.mData); break; } } }