Android ProgressBar在等待api时未显示

Android ProgressBar在等待api时未显示,android,api,kotlin,loading,android-progressbar,Android,Api,Kotlin,Loading,Android Progressbar,我试图在从API获取数据时显示progressbar,但它从不显示。我假设我只是在错误的地方调用了progressbar,但到目前为止我所做的任何事情都没有解决这个问题。下面是活动的代码。 我只希望progressbar在加载数据之前可见 package com.hmadland.paranoia import android.content.Context import android.content.Intent import android.support.v7.app.AppCompat

我试图在从API获取数据时显示progressbar,但它从不显示。我假设我只是在错误的地方调用了progressbar,但到目前为止我所做的任何事情都没有解决这个问题。下面是活动的代码。 我只希望progressbar在加载数据之前可见

package com.hmadland.paranoia
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.activity_profile.*
import org.json.JSONObject
import com.android.volley.AuthFailureError
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.hmadland.paranoia.R.layout.activity_profile

class Profile : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)
        //get token from login
        val token = intent.getStringExtra("token")
        //get email to find PlayerID in sharedPref
        val email = intent.getStringExtra("email")
        //get PlayerID from sharedPref
        val mypref = getSharedPreferences(email, Context.MODE_PRIVATE)
        val PlayerID = mypref.getString(email, "")
        val progressBar: ProgressBar = this.progressBar1

            // task is run on a thread
            Thread(Runnable {
                // dummy thread mimicking some operation whose progress cannot be tracked


                // display the indefinite progressbar
                this@Profile.runOnUiThread(java.lang.Runnable {
                    progressBar1.visibility = View.VISIBLE
                })
                // performing operation
                try {
                    var url = "https://elimination.azurewebsites.net/api/Players/GetPlayer?id="+ PlayerID
                    val que = Volley.newRequestQueue(this@Profile)

                    val jsonObjectRequest = JsonObjectRequest(
                            Request.Method.GET, url, null,
                            Response.Listener<JSONObject> { response ->
                                //get UserName
                                userName.text = response.get("UserName").toString()
                                //get profile
                                profile.text = response.get("Profile").toString()
                                //get photo
                                Glide.with(this).load(response.get("PhotoUrl")).into(ProfilePic)
                                //round photo
                                Glide.with(this).load(response.get("PhotoUrl")).apply(RequestOptions.circleCropTransform()).into(ProfilePic)
                            },
                            Response.ErrorListener { error -> Log.e("error is ", "" + error) }
                    )

                    //This is for Headers If Needed
                    @Throws(AuthFailureError::class)
                    fun getHeaders(): Map<String, String> {
                        val params = HashMap<String, String>()
                        params.put("Content-Type", "application/json; charset=UTF-8")
                        params.put("token", token)
                        return params
                    }
                    que.add(jsonObjectRequest)

                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }

                // when the task is completed, make progressBar gone
                this@Profile.runOnUiThread(java.lang.Runnable {
                    progressBar1.visibility = View.GONE
                })
            }
            ).start()

        ///////////buttons to other views//////////////////////////////
        //go to current target view
        CurrentGame.setOnClickListener{
            val intent = Intent(this, Target::class.java)
            intent.putExtra("token", token)
            intent.putExtra("email", email)
            startActivity(intent)
        }

        Inventory.setOnClickListener{
            val intent = Intent(this, com.hmadland.paranoia.Inventory::class.java)
            startActivity(intent)
        }

        coming.setOnClickListener{
            val intent = Intent(this, UpcomingGames::class.java)
            startActivity(intent)
        }
        ////////////////////////////////////////////////////////////////////
    }
}
package com.hmadland.paranoia
导入android.content.Context
导入android.content.Intent
导入android.support.v7.app.AppActivity
导入android.os.Bundle
导入android.util.Log
导入android.view.view
导入android.widget.ProgressBar
导入com.android.volley.Request
导入com.android.volley.Response
导入com.android.volley.toolbox.JsonObjectRequest
导入com.android.volley.toolbox.volley
导入kotlinx.android.synthetic.main.activity\u配置文件*
导入org.json.JSONObject
导入com.android.volley.AuthFailureError
导入com.bumptech.glide.glide
导入com.bumptech.glide.request.RequestOptions
导入com.hmadland.paranoia.R.layout.activity\u配置文件
类配置文件:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\U配置文件)
//从登录中获取令牌
val token=intent.getStringExtra(“token”)
//获取电子邮件以在sharedPref中查找PlayerID
val email=intent.getStringExtra(“电子邮件”)
//从sharedPref获取PlayerID
val mypref=getSharedReferences(电子邮件、上下文.MODE_PRIVATE)
val PlayerID=mypref.getString(电子邮件,“”)
val progressBar:progressBar=this.progressBar1
//任务在线程上运行
线程(可运行{
//伪线程模拟某些无法跟踪其进度的操作
//显示进度条
this@Profile.runOnUiThread(java.lang.Runnable){
progressBar1.visibility=View.VISIBLE
})
//执行操作
试一试{
变量url=”https://elimination.azurewebsites.net/api/Players/GetPlayer?id=“+PlayerID
val que=Volley.newRequestQueue(this@Profile)
val jsonObjectRequest=jsonObjectRequest(
Request.Method.GET,url,null,
Response.Listener{Response->
//获取用户名
userName.text=response.get(“userName”).toString()
//获取配置文件
profile.text=response.get(“profile”).toString()
//拍照
Glide.with(this.load)(response.get(“PhotoUrl”))进入(ProfilePic)
//圆形照片
Glide.with(this).load(response.get(“PhotoUrl”)).apply(RequestOptions.circleCropTransform()).into(ProfilePic)
},
Response.ErrorListener{error->Log.e(“error is”,“”+error)}
)
//如果需要,这适用于标题
@抛出(AuthFailureError::类)
fun getHeaders():映射{
val params=HashMap()
参数put(“内容类型”,“应用程序/json;字符集=UTF-8”)
参数put(“令牌”,令牌)
返回参数
}
添加(jsonObjectRequest)
}捕获(e:中断异常){
e、 printStackTrace()
}
//任务完成后,使progressBar消失
this@Profile.runOnUiThread(java.lang.Runnable){
progressBar1.visibility=View.GONE
})
}
).start()
///////////其他视图的按钮//////////////////////////////
//转到当前目标视图
CurrentGame.setOnClickListener{
val intent=intent(这个,Target::class.java)
intent.putExtra(“令牌”,令牌)
intent.putExtra(“电子邮件”,电子邮件)
星触觉(意图)
}
Inventory.setOnClickListener{
val intent=intent(这个,com.hmadland.paranoia.Inventory::class.java)
星触觉(意图)
}
coming.setOnClickListener{
val intent=intent(这是UpcomingGames::class.java)
星触觉(意图)
}
////////////////////////////////////////////////////////////////////
}
}
这是布局中的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.hmadland.paranoia.Profile">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/colorPrimary"
        android:fitsSystemWindows="true"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="101dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="24dp"
            android:paddingRight="24dp"
            android:paddingTop="56dp"
           >


            <ProgressBar
                android:id="@+id/progressBar1"
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:padding="10dp"

                />

            <ImageView
                android:id="@+id/ProfilePic"
                android:layout_width="match_parent"
                android:layout_height="132dp"
                android:layout_centerHorizontal="true"
                android:adjustViewBounds="true"
                android:maxHeight="300px"
                android:maxWidth="450px"
                android:minHeight="300px"
                android:minWidth="450px" />

            <TextView
                android:id="@+id/userName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="@color/colorAccent"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/profile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="12dp"
                android:paddingTop="12dp"
                android:textColor="@color/colorAccent"
                android:textSize="20sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center"
                >

                <Button
                    android:id="@+id/CurrentGame"
                    android:layout_width="90dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:background="@drawable/round"
                    android:fontFamily="monospace"
                    android:text="Target"
                    android:textColor="@color/colorAccent"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/Inventory"
                    android:layout_width="90dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:background="@drawable/round"
                    android:fontFamily="monospace"
                    android:text="Stats"
                    android:textColor="@color/colorAccent"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center">

                <Button
                    android:id="@+id/coming"
                    android:layout_width="90dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:shape="oval"
                    android:background="@drawable/round"
                    android:fontFamily="monospace"
                    android:text="Games"
                    android:textColor="@color/colorAccent"
                    android:textSize="18sp"
                    android:textStyle="bold" />
                <Button
                    android:id="@+id/edit"
                    android:layout_width="90dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:background="@drawable/round"
                    android:fontFamily="monospace"
                    android:text="Edit"
                    android:textColor="@color/colorAccent"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</android.support.constraint.ConstraintLayout>

我认为这与
Volley.newRequestQueue(this@Profile)
返回通过其
add
方法将要执行的请求排队的方法。但是,
add
方法会立即返回,而无需等待调用本身的执行

因为在您的代码中,您直接在
add
调用之后运行进度条可见性的更改,所以您可能根本看不到进度条的出现(因为整个代码运行得太快了)。您可能希望在
Response.Listener
回调中隐藏该条(也可能在
Response.ErrorListener
中),因为这些是Volley在完成时调用的回调(无论是否有错误)


因此,您将请求排队并显示进度条,当Volley完成时,它将通过回调让您知道,这将是再次隐藏进度条的地方。

您是指Spinner()还是ProgressBar?您应该在包含progressBar1的位置添加xml布局