当我按下在片段中激活异步任务的按钮时,应用程序停止工作

我在我的应用程序中打开一个新片段,我想计算价格并每秒打印一次(通过 asyncTask),但是当我运行应用程序时停止工作。你能帮我找出问题吗?


public class RandomDrive extends Fragment {

private Chronometer chronometer;

private Button start;

private Button stop;

private TextView price;

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_random_drive, container, false);

    chronometer = (Chronometer) v.findViewById(R.id.chronometer);

    price = v.findViewById(R.id.see_price);

    class SyncTaskCounter extends AsyncTask<Void, Void, Void> {

        @Override

        protected Void doInBackground(Void... voids) {

            double price = 0;


            while (!isCancelled()) {

                price = price + 0.05;

                try {

                    Thread.sleep(1000);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                printPrice(price);

            }

            return null;

        }

        @Override

        protected void onCancelled() {

            super.onCancelled();

        }

    }

    final SyncTaskCounter asyncTaskCount;

    asyncTaskCount = new SyncTaskCounter();

    start = (Button) v.findViewById(R.id.start);

    start.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {


            chronometer.setBase(SystemClock.elapsedRealtime());

            chronometer.start();


            asyncTaskCount.execute();

        }

    });

    stop = (Button) v.findViewById(R.id.stop);

    stop.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            chronometer.stop();

            asyncTaskCount.cancel(true);


        }

    });

    return v;

}


public void printPrice(double price1) {

    price.setText("the price is: " + price1 + " shekel");


}

}

问题出在 asyncTask 中,因为当我在没有 asyncTask 的情况下运行该函数并仅在最后计算价格时 - 应用程序运行。


Smart猫小萌
浏览 141回答 1
1回答

繁星coding

在 Android 上,您必须在 UI 线程上运行所有 UI 代码。AsyncTask 中的代码在其他线程中执行,因此它不能调用 UI 方法。为此,您应该使用 Handler(代码将在 UI 线程中调用): https ://developer.android.com/reference/android/os/Handlerfinal Handler h=new Handler();h.postDelayed(new Runnable() {&nbsp; &nbsp;public void run() {&nbsp; &nbsp; &nbsp; printPrice(price);&nbsp; &nbsp; &nbsp; price = price + 0.05;&nbsp; &nbsp; &nbsp; h.postDelayed(this, 1000); // call for next update&nbsp; &nbsp;}}, 1000);我你必须使用 AsyncTask,那么你应该从方法更新 UI:onProgressUpdate https://developer.android.com/reference/android/os/AsyncTaskclass SyncTaskCounter extends AsyncTask<Void, Double, Void> {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Void doInBackground(Void... voids) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double price = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (!isCancelled()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; price = price + 0.05;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; publishProgress(price); // this instructs to call onProgressUpdate from UI thread.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onProgressUpdate(Double... price) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printPrice(price[0]);&nbsp; // this is called on UI thread&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onCancelled() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onCancelled();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java