【问题标题】:deprecated thread methods are not supported不支持弃用的线程方法
【发布时间】:2012-01-30 23:49:59
【问题描述】:

我正在制作一个项目,我需要在其中显示主页,当主页显示时,之后或继续显示 3 到 5 秒我的另一个欢迎自定义对话框。但是这样做,会发生以下错误,但我的应用程序并没有停止工作.. LogCat 显示这些错误。 申请代码:

  final Dialog d=new Dialog(Main.this);
    d.setContentView(R.layout.SplashScreen);
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                d.show();

                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }
            } catch(InterruptedException e) {
                // do nothing
            } finally {
                d.cancel();
                    stop();
            }
        }
    };
    splashTread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _active = false;
    }
    return true;
}

LogCat 中的错误:

12-30 14:54:54.044: E/global(1232): Deprecated Thread methods are not supported.
12-30 14:54:54.044: E/global(1232): java.lang.UnsupportedOperationException.
12-30 14:54:54.044: E/global(1232):     at java.lang.VMThread.stop(VMThread.java:85)
12-30 14:54:54.044: E/global(1232):     at java.lang.Thread.stop(Thread.java:1280)
12-30 14:54:54.044: E/global(1232):     at java.lang.Thread.stop(Thread.java:1247)
12-30 14:54:54.044: E/global(1232):     at com.droidnova.android.SplashScreen$1.run(SplashScreen.java:35)

【问题讨论】:

    标签: android


    【解决方案1】:

    把这个最好的方式做闪屏

    int SPLASH_TIME = 1300;
        Handler HANDLER = new Handler();
    
                        // thread for displaying the SplashScreen
                        HANDLER.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                             finish();
                             startActivity (new Intent(getApplicationContext(),Alpha.class));
                            }
                          }, SPLASH_TIME);
    

    【讨论】:

    【解决方案2】:

    此链接会告诉您确切的问题是什么,以及如何解决它:

    What is this log, when I coded thread.stop()?

    Thread.stop 是一个弃用的 API,而弃用的线程方法不是 在安卓中支持。因此它抛出一个 UnsupportedOperationException。

    答案是不要使用 Thread.stop - 在一个 更优雅的方式,例如通过设置线程的标志 定期检查。

    此链接讨论为什么不推荐使用 thread.stop()(很久以前在 Java 中不推荐使用,而不仅仅是 Android!):

    http://docs.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

    【讨论】:

      【解决方案3】:

      在 Android 中,最好使用 Handler 来管理 ThreadRunnables

      创建一个Handler实例

      Handler handler = new Handler();
      

      创建一个可运行线程

      Runnable runnable = new Runnable() {
      
              @Override
              public void run() {
                  Log.d("runnable started", "inside run");
                  handler.removeCallbacks(runnable);
                  handler.postDelayed(runnable, 1000);
              }
          };
      

      并使用 Handler 启动 Runnable

      handler.postDelayed(runnable, 1000);
      

      并停止 Runnable 使用

      handler.removeCallbacks(runnable);
      

      【讨论】:

      • 我从外部使用处理程序.. 在放入 run() 方法时,它显示可运行未初始化错误
      • 你必须将顶部的 Handler 声明为全局并使用它。
      猜你喜欢
      • 2019-12-17
      • 2020-04-15
      • 2021-06-11
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      相关资源
      最近更新 更多