2017-09-16 72 views
2

我有Android应用的一个例子来更新使用处理器工作线程主线程进度,它的正常工作,但是当我尝试内更新进度工作者线程没有处理程序,它仍然有效,尽管它应该会崩溃。因为根据Android文档,UI线程不安全,不能直接从工作线程访问。从工作线程主线程有时更新进度条不崩溃的Android应用程序

***更新 对不起,我把错误activity_main.xml中。 这是我正在使用的正确的一个。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.riaz.handlerexample.MainActivity"> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:indeterminate="false" 
     android:max="100" 
     android:progress="0" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="8dp" /> 
</android.support.constraint.ConstraintLayout> 

***更新

这里是activity_main.xml中。 **更新。错布局文件,请忽略Question

如今

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:indeterminate="true" 
    android:max="10" 
    android:padding="4dip" > 
</ProgressBar> 

    <TextView 
     android:id="@+id/tv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Progress percentage" 
     android:textSize="16sp"/> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="startProgress" 
     android:text="Start" 
     android:textSize="16sp"/> 

</LinearLayout> 

Java代码

import android.os.Handler; 
import android.os.Message; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ProgressBar; 
import android.widget.Toast; 


public class MainActivity extends AppCompatActivity { 

     Thread thread; // work as a background thread 
     Handler handler; // handle main thread messages from worker thread 
     ProgressBar progressBar; // on UI/main thread 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     progressBar = (ProgressBar) findViewById(R.id.progressBar); 


     Toast.makeText(MainActivity.this, "OnCreate Thread Name = " 
         +Thread.currentThread().getName(), 
       Toast.LENGTH_LONG).show(); // Thread Name = main 

     System.out.println("OnCreate Method,Thread Name= " + 
       Thread.currentThread().getName()); // Thread Name = main 


     thread = new Thread(new MyThread());// creating a background thread 
     thread.start(); // starting the background thread. it will execute run() method in 
         // MyThread class 

     handler = new Handler(){ //handles the incoming messages from background thread 

      @Override // receives message from worker thread sendmessage(message) 
      public void handleMessage(Message msg) { 

       System.out.println("Inside Handler,Thread Name= " + 
         Thread.currentThread().getName()); //thread Name = main 

       // progressBar.setProgress(msg.arg1); 
      } 
     }; 


    } 


    // this is background/worker thread 

    class MyThread implements Runnable 
    { 
     @Override 
     public void run() { 

      Toast.makeText(MainActivity.this, "Worker Thread Name = " 
          +Thread.currentThread().getName(), 
        Toast.LENGTH_LONG).show(); // Thread Name = main 

      for (int i=0; i<100;i++) 
      { 

       System.out.println("Worker Thread,Thread Name= " + 
         Thread.currentThread().getName()); 

       // Message message = Message.obtain(); // blank empty message 
       // message.arg1 = i; 
       // handler.sendMessage(message); //calling main thread 

       progressBar.setProgress(i); 


       //create delay to see the progress on progressbar 

       try { 
        Thread.sleep(300); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 
+1

兄弟公司崩溃'了java.lang.RuntimeException:无法内螺纹创建处理程序尚未调用Looper.prepare()' – akhilesh0707

+1

您从main.xml中初始化'progressBar'但在主XML内部,它是'progressBar1' – akhilesh0707

+0

@ akhilesh0707是的,你是对的,有时它崩溃,有些时候没有。一旦它崩溃,编辑代码后继续崩溃,但一旦我重新启动android studio和模拟器​​并再次运行它,那么它工作正常,并继续工作正常,除非我更改代码并重新启动android工作室和模拟器。 –

回答

2

作为CommonsWare答案,ProgressBar有逻辑,允许setProgress()在后台线程中调用。它会检查您正在使用的线程,并根据需要自行执行Runnablepost()

回答参考link