代码之家  ›  专栏  ›  技术社区  ›  jordanthompson

Android应用程序无法显示进度条

  •  0
  • jordanthompson  · 技术社区  · 8 年前

    以下是活动中progressbar的部分:

    <ProgressBar
        android:id="@+id/progressBarLogin"
        android:indeterminate="true"
        android:layout_centerInParent="true"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/editText_password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    以下是OnCreate的代码:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myProgressBar = findViewById(R.id.progressBarLogin);
        myProgressBar.setVisibility(View.GONE); 
        // progress bar is hidden (yay!)
        // other stuff goes here
     }
    

    下面是按钮回调的代码:

        final Button loginButton = findViewById(R.id.buttonLogin);
        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // login button was clicked
                try {
                    // myCommandSender is a thread:
                    myCommandSender.start()        
                    waitForCommandSender();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    

    以下是此线程中等待MyCommand Sender完成的代码:

        private void waitForCommandSender() {
        myProgressBar.setVisibility(View.VISIBLE);
        while (myCommandSender.isAlive()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myProgressBar.setVisibility(View.GONE);
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Sivakumar    8 年前

    我的建议是,首先,你需要 调用waitForCommandSender();方法 之后,你需要开始线程

     final Button loginButton = findViewById(R.id.buttonLogin);
      loginButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // login button was clicked
            try {
                // myCommandSender is a thread:
                //call the method first and start thread next
                 waitForCommandSender();
                myCommandSender.start()      
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    
        2
  •  0
  •   Viswanath Kumar Sandu    8 年前

    活动的onCreate()中缺少setContentView(R.layout.________________。因此,您永远无法访问UI。请将其添加到活动中并进行检查