0

次のコードは、行に致命的な例外非同期タスク#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;
    }
4

3 に答える 3

5

バックグラウンドスレッドからUIの状態を変更することはできません。メインスレッドで呼び出される
AsyncTaskonPreExecute()とメソッド。 見てください: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;
        }
    }
}
于 2013-02-04T10:16:52.753 に答える
0

UIスレッドからウィジェットを変更する必要があります。メソッドonProgressUpdateOnPostExecuteはUItheradで実行されます。したがって、コードを後者に移動することを検討してください。

于 2013-02-04T10:18:42.583 に答える
0

UIの変更をに移動しますonPostExecute

boolean success = false;

protected Void doInBackground(Void... params) {
      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) {
      if (success) {
         //this one causes Async Task exception
            v1.setEnabled(false);
        } else {

        }
       mRegisterTask1 = null;
  }
于 2013-02-04T10:16:01.160 に答える