在不支持的线程操作异常

编程入门 行业动态 更新时间:2024-10-14 14:16:07
本文介绍了在不支持的线程操作异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经做了一定的过程一个线程,而我同时运行这个线程与进度对话框。当程序完成,进度对话框将消失。直到这没有问题,但是当用户将preSS回电话两次按钮,活动会破坏我的过程中应停止。所以我在的onDestroy停止线程()。但应用程序崩溃在 mt.stop()我的code和错误日志为波纹管。请给我任何解决方案,也可能的话我想这背后的原因。  使用标志,过程仅在每次迭代开始时检查站。我想突出停止进程。

code

键b;ProgressDialog PD = NULL;MyThread的吨;@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    B =(按钮)findViewById(R.id.btn_start);    b.setOnClickListener(本);}公共无效的onClick(视图v){    PD = ProgressDialog.show(这一点,一,二,真实,真实);    MT =新MyThread的();    mt.start();}类MyThread的继承Thread {    @覆盖    公共无效的run(){        //假设这个循环是我的过程。        对(INT I = 0; I&小于10;我++){            尝试{                视频下载(1000);            }赶上(InterruptedException的E){                // TODO自动生成catch块                e.printStackTrace();            }            Log.v(log_tag,+ I);        }        Log.v(log_tag,COmplited处理);        pd.dismiss();        super.run();    }}@覆盖保护无效的onDestroy(){    Log.v(log_tag,的OnDestroy);    pd.dismiss();    mt.stop();    super.onDestroy();}

错误日志猫;

11-09 17:26:45.313:E /全局(594):德precated线程的方法不被支持。11-09 17:26:45.313:E /全局(594):java.lang.UnsupportedOperationException11-09 17:26:45.313:E /全局(594):在java.lang.VMThread.stop(VMThread.java:85)11-09 17:26:45.313:E /全局(594):在java.lang.Thread.stop(Thread.java:1379)11-09 17:26:45.313:E /全局(594):在java.lang.Thread.stop(Thread.java:1344)11-09 17:26:45.313:E /全局(594):在com.example.threaddemo.MainActivity.onDestroy(MainActivity.java:53)11-09 17:26:45.313:E /全局(594):在android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642​​)11-09 17:26:45.313:E /全局(594):在android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)11-09 17:26:45.313:E /全局(594):在android.app.ActivityThread.access $ 2900(ActivityThread.java:125)11-09 17:26:45.313:E /全局(594):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2066)11-09 17:26:45.313:E /全局(594):在android.os.Handler.dispatchMessage(Handler.java:99)11-09 17:26:45.313:E /全局(594):在android.os.Looper.loop(Looper.java:123)11-09 17:26:45.313:E /全局(594):在android.app.ActivityThread.main(ActivityThread.java:4627)11-09 17:26:45.313:E /全局(594):在java.lang.reflect.Method.invokeNative(本机方法)11-09 17:26:45.313:E /全局(594):在java.lang.reflect.Method.invoke(Method.java:521)11-09 17:26:45.313:E /全局(594):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)11-09 17:26:45.313:E /全局(594):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)11-09 17:26:45.313:E /全局(594):在dalvik.system.NativeStart.main(本机方法)

解决方案

请参考我的解决方案在:android杀行动吧事件特定线程

使用,而不是线程服务器一个TimerTask您purponse更好更的解决方案中。

甚至取消将工作:)

P.S:它还具有信息为什么停止工作不适合你。

I have made a thread for certain process, and I am running this thread with Progress Dialog simultaneously. when process complete, Progress Dialog will dismiss. until this there is no problem but when user will press back button of phone two times, activity will destroy and my process should be stop. so I have stopped thread in onDestroy(). but app is crashing at mt.stop() my code and error log is as bellow. please give me any solution and also if possible I want reason behind this. Using flag, process will check for stop only at start of each iteration. and I want to stop process eminently.

code

Button b; ProgressDialog pd = null; MyThread mt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button) findViewById(R.id.btn_start); b.setOnClickListener(this); } public void onClick(View v) { pd = ProgressDialog.show(this, "one", "two", true, true); mt = new MyThread(); mt.start(); } class MyThread extends Thread { @Override public void run() { // assume this loop is my process. for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.v("log_tag", "" + i); } Log.v("log_tag", "process COmplited"); pd.dismiss(); super.run(); } } @Override protected void onDestroy() { Log.v("log_tag", "OnDestroy"); pd.dismiss(); mt.stop(); super.onDestroy(); }

error in log cat;

11-09 17:26:45.313: E/global(594): Deprecated Thread methods are not supported. 11-09 17:26:45.313: E/global(594): java.lang.UnsupportedOperationException 11-09 17:26:45.313: E/global(594): at java.lang.VMThread.stop(VMThread.java:85) 11-09 17:26:45.313: E/global(594): at java.lang.Thread.stop(Thread.java:1379) 11-09 17:26:45.313: E/global(594): at java.lang.Thread.stop(Thread.java:1344) 11-09 17:26:45.313: E/global(594): at com.example.threaddemo.MainActivity.onDestroy(MainActivity.java:53) 11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642) 11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673) 11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.access$2900(ActivityThread.java:125) 11-09 17:26:45.313: E/global(594): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 11-09 17:26:45.313: E/global(594): at android.os.Handler.dispatchMessage(Handler.java:99) 11-09 17:26:45.313: E/global(594): at android.os.Looper.loop(Looper.java:123) 11-09 17:26:45.313: E/global(594): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-09 17:26:45.313: E/global(594): at java.lang.reflect.Method.invokeNative(Native Method) 11-09 17:26:45.313: E/global(594): at java.lang.reflect.Method.invoke(Method.java:521) 11-09 17:26:45.313: E/global(594): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-09 17:26:45.313: E/global(594): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-09 17:26:45.313: E/global(594): at dalvik.system.NativeStart.main(Native Method)

解决方案

Please refer my solution at: android kill specific thread on action bar event

The solution of using a TimerTask instead of the thread can server your purponse better and more optimized.

Even the cancellation would work :)

P.S: It also has information why stop is not working for you.

更多推荐

在不支持的线程操作异常

本文发布于:2023-11-12 10:55:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不支持   线程   异常   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!