【问题标题】:startB.setVisibility(View.VISIBLE) crashes on Android StudioAndroid Studio 上的 startB.setVisibility(View.VISIBLE) 崩溃
【发布时间】:2018-05-08 17:50:46
【问题描述】:

尝试在进度条达到最大值 (100%) 后显示按钮,但应用程序由于某种原因崩溃。我可以让它改变颜色并在进度条加载后使其不可见,但由于某种原因不可见,请帮忙!

这是class.java代码:

int progress = 0;
ProgressBar simpleProgressBar;
Button startB = null;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enteredring_page);
    // initiate progress bar and start button
    simpleProgressBar = (ProgressBar) findViewById(R.id.loadingfightbar);
    setProgressValue(progress);

    startB  = (Button)findViewById(R.id.showfight);
  //  startB.setVisibility(View.VISIBLE);


    View overlay = findViewById(R.id.inringlayout);

    overlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);


    VideoView mVideoView = (VideoView) findViewById(R.id.videoView);
    String uriPath = "android.resource://com.seth.boxingadventure.boxerapp/" + R.raw.hayahadvid;
    Uri uri = Uri.parse(uriPath);
    mVideoView.setVideoURI(uri);
    mVideoView.requestFocus();
    mVideoView.start();



}

private void setProgressValue(final int progress) {

    // set the progress
    simpleProgressBar.setProgress(progress);

    // thread is used to change the progress value
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);

                if(simpleProgressBar.getProgress() == simpleProgressBar.getMax())
                {
                   // startB.setVisibility(View.VISIBLE);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            setProgressValue(progress + 10);


        }
    });
    thread.start();

}

}

还有xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@android:color/white"
     android:id="@+id/inringlayout"
     android:weightSum="1">


<VideoView
    android:id="@+id/videoView"
    android:layout_width="fill_parent"
    android:layout_height="318dp"
    android:layout_weight="0.87" />

<ProgressBar
    android:id="@+id/loadingfightbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="300dp"
    android:layout_height="wrap_content"

    android:angle="270"
    android:centerColor="#C27452"
    android:centerY="0.75"
    android:endColor="#050505"


    android:startColor="#F0500A"
    android:visibility="visible"
    tools:visibility="visible"
    android:layout_below="@+id/videoView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="81dp"

    android:max="100"
    android:progress="50"
    />

<Button
    android:id="@+id/showfight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:visibility="invisible"
    android:layout_marginBottom="17dp" />

【问题讨论】:

    标签: java android visibility


    【解决方案1】:

    在 Android 中,主线程(UI 线程)只能访问 UI 组件,即您不能从非 UI 线程更改 UI 元素,如果您想从后台线程访问 UI 组件,请使用 runOnUiThread。
    试试下面的代码:

        private void setProgressValue(final int progress) {
    
            // set the progress
            simpleProgressBar.setProgress(progress);
    
            // thread is used to change the progress value
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
    
                        if(simpleProgressBar.getProgress() == simpleProgressBar.getMax())
                        {
                             runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                               startB.setVisibility(View.VISIBLE);
                                            }
                                        });
    
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    setProgressValue(progress + 10);
    
    
                }
            });
            thread.start();
    
        }
    

    【讨论】:

    • 漂亮,现在完美运行!先生,您值得拥有一颗金星!谢谢
    【解决方案2】:

    在android中,只有UI/主线程可以更新任何UI组件,所以你不能从后台线程更新UI,会导致崩溃

    Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
    
                    if(simpleProgressBar.getProgress() == simpleProgressBar.getMax())
                    {
                       // startB.setVisibility(View.VISIBLE);
                      // ^^^^^^^^^^^^^^^^^^ crash
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                setProgressValue(progress + 10);
    
    
            }
        });
    

    解决方案:使用AsynchTaskHandler or runOnUiThread

    【讨论】:

      【解决方案3】:

      使用 runOnUiThread 像:

      private void setProgressValue(final int progress) {
      
          // set the progress
          simpleProgressBar.setProgress(progress);
      
          // thread is used to change the progress value
          Thread thread = new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      Thread.sleep(1000);
      
                      if(simpleProgressBar.getProgress() == simpleProgressBar.getMax()) {
                          runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  startB.setVisibility(View.VISIBLE);
                              }
                          });
                      }
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  setProgressValue(progress + 10);
              }
          });
          thread.start();
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-03
        • 1970-01-01
        相关资源
        最近更新 更多