成功doinbackground调用时禁用Android禁用按钮

sel*_*tch 0 android asynchronous fatal-error

以下代码在行上提供致命异常异步任务#2 v1.setEnabled(false).

这意味着在成功通话时禁用按钮.在v.setEnabled(false);之前的后台任务运行良好.请帮忙 :(

public void onClick(View v) {
    Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
    //this one would work
    //v.setEnabled(false);

    final View v1=v;
    mRegisterTask1 = new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground(Void... params) {
        boolean success = 
            ServerUtilities.receipt (((String)v1.getTag()).substring(3),"acknowledged");

        if (success) {
            //this one causes Async Task exception
            v1.setEnabled(false);
        } 
        else {
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mRegisterTask1 = null;
    }
Run Code Online (Sandbox Code Playgroud)

EvZ*_*EvZ 5

您无法从后台线程更改UI状态.在主线程中调用
AsyncTask onPreExecute()onPostExecute()方法.
看一看 :

public void onClick(View v) {
    Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
    //this one would work
    //v.setEnabled(false);


    final View v1=v;
    mRegisterTask1 = new AsyncTask<Void, Void, Boolean>() {

        @Override
        protected Boolean doInBackground(Void... params) {
            boolean success =
                    ServerUtilities.receipt (((String)v1.getTag()).substring(3),"acknowledged");


            return success;
        }

        @Override
        protected void onPostExecute(Bolean result) {

        if (result) {
                //this one causes Async Task exception
                v1.setEnabled(false);
            } else {

            }

            mRegisterTask1 = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)