我在构建应用程序时没有遇到任何错误,但当应用程序最终启动时,它在从JsonArrayRequest()
函数获得响应后立即崩溃。
这些是我的日志:logcat 1logcat 2显示器
FATAL EXCEPTION: main
Process: com.example.scoremusplayer, PID: 19239
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:122)
at com.bumptech.glide.Glide.get(Glide.java:128)
at com.bumptech.glide.Glide.getRetriever (Glide.java:510)
at com.bumptech.glide.Glide.with(Glide.java:564)
at com.example.scoremusplayer.scoreExplore.adapter.AdapterForScoreListForAllScoreFragmentForRecyclerView.onBindViewHolder
at com.example.scoremusplayer.scoreExplore.adapter.AdapterForScoreListForAllScoreFragmentForRecyclerView.onBindViewHolder
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder (RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder (RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline (RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.GridLayoutManager.LayoutChunk (GridLayoutManager.java:561)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
以下是片段中的代码:
class AllScoresFragment : Fragment() {
companion object {
lateinit var tempList : ArrayList<Score>
fun newInstance() = AllScoresFragment()
}
private lateinit var binding: FragmentAllScoresBinding
private lateinit var viewModel: AllScoresViewModel
private lateinit var progressDialog : ProgressDialog
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentAllScoresBinding.inflate(inflater)
return binding.root
// return inflater.inflate(R.layout.fragment_all_scores, container, false)
}
@Deprecated("Deprecated in Java")
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this)[AllScoresViewModel::class.java]
binding.include.recyclerViewForScoreContent.setHasFixedSize(true)
binding.include.recyclerViewForScoreContent.layoutManager = GridLayoutManager(context, 3)
retrieveTwo()
binding.swipeToRefresh.setOnRefreshListener {
retrieveTwo()
binding.swipeToRefresh.isRefreshing = false
}
}
我可以感觉到问题是来自retrieveTwo()
函数中的JsonArrayRequest
得到响应后发生的事情,但我似乎不知道我在那里做错了什么。
以下是retrieveTwo()
函数:
fun retrieveTwo() {
tempList = ArrayList()
progressDialog = ProgressDialog(context)
progressDialog.setMessage("Loading...")
progressDialog.show()
val pdfSiteUrl = "https://..."
// Get a RequestQueue
AllScoresFragment().context?.let { MySingleton.getInstance(it).requestQueue }
val jsonArrayRequest = JsonArrayRequest(
Request.Method.GET, pdfSiteUrl, null,
{ response ->
//Toast the response to know if response is actually gotten
Toast.makeText(context, response.toString(), Toast.LENGTH_LONG).show()
var jo : JSONObject
try {
for (i in 0 until response.length()){
jo = response.getJSONObject(i)
val id = jo.getInt("id")
val name:String = jo.getString("name")
val composer:String = jo.getString("composer")
val style:String = jo.getString("style")
val theme:String = jo.getString("theme")
val album:String = jo.getString("album")
val lang:String = jo.getString("lang")
val thumbnailUrl:String = jo.getString("thumbnail_url")
val pdfUrl: String = jo.getString("pdf_url")
val score = Score(
id = id,
name = name,
composer = composer,
style = style,
theme = theme,
album = album,
lang = lang,
thumbnail_url = pdfSiteUrl + thumbnailUrl,
pdf_url = pdfSiteUrl + pdfUrl
)
tempList.add(score)
val adapter =
context?.let {
AdapterForScoreListForAllScoreFragmentForRecyclerView(it,
tempList) }
binding.include.recyclerViewForScoreContent.adapter = adapter
progressDialog.hide()
}
} catch (e : JSONException){
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
progressDialog.hide()
}
},
{ error ->
Toast.makeText(context,
error.message,
Toast.LENGTH_SHORT)
.show()
progressDialog.hide()
}
)
// Access the RequestQueue through my singleton class.
context?.let { MySingleton.getInstance(it).addToRequestQueue(jsonArrayRequest) }
}
以下是我的Recyclerview适配器的外观:
class AdapterForScoreListForAllScoreFragmentForRecyclerView(
private val context: Context,
private val scoreList: ArrayList<Score>
) : RecyclerView.Adapter<AdapterForScoreListForAllScoreFragmentForRecyclerView.ViewHolder>() {
class ViewHolder(binding: ScoreListForRecycleBinding) : RecyclerView.ViewHolder(binding.root){
val pdfThumbnailImage = binding.thumbnailForScore
val songTitle = binding.scoreTitleNameOfAllScoresFragment
val composerName = binding.nameOfComposerForAllScoresFragment
val genre = binding.styleForAllScoresFragment
//val for root
val root = binding.root
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
: ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val scoreList = ScoreListForRecycleBinding.inflate(layoutInflater, parent, false)
return ViewHolder(scoreList)
}
override fun getItemCount(): Int {
return scoreList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val scoreName = scoreList[position].name
val scoreComposer = scoreList[position].composer
val genre = scoreList[position].style
val pdfUrl = scoreList[position].pdf_url
val thumbnailImage = scoreList[position].thumbnail_url
holder.songTitle.text = scoreName
holder.composerName.text = scoreComposer
holder.genre.text = genre
if (pdfUrl.isNotEmpty()){
Glide
.with(FragmentActivity())
.load(thumbnailImage)
.placeholder(R.drawable.scoremus_icon_slash)
.into(holder.pdfThumbnailImage)
}
holder.root.setOnClickListener {
holder.root.isLongClickable = true
Toast.makeText(
context,
"opening \"${scoreName.uppercase()} composed by $scoreComposer\"...",
Toast.LENGTH_SHORT)
.show()
val intent = Intent(context, PdfActivity::class.java)
intent.putExtra("index", position)
intent.putExtra("pdfPath", scoreList[position].pdf_url)
ContextCompat.startActivity(context, intent, null)
}
}
}
帮帮我
这就是我想做的
1.从https源请求json数组
1.获取json数组中每个元素的json对象
1.在回收视图中显示对象
但是我的应用程序在得到json数组的响应后崩溃了
1条答案
按热度按时间nafvub8i1#
您正在创建一个
Fragment Activity
示例并将其传递给未附加到应用程序context
的glide
。当glide
尝试访问applicationContext
时,它将引发NPE
。您可以传递任何保持器视图或
context
使用视图
使用上下文