android - 尝试在空对象引用上调用虚拟方法“void android.view.View.setVisibility(int)”
问题描述
我正在尝试在片段中实现进度条,但它给了我这个错误。我正在调用一个 api,它为我提供数据,在获取数据之前,我想运行进度条。
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.view.View.setVisibility(int)”
这是代码
class GeneralFragment : Fragment() {
private lateinit var mAdapter: NewsListAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
fetchdata("https://news-api-don.herokuapp.com/api/v1?apiKey=20d14506791144cc8b424549c42068c0")
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_general, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
mAdapter = NewsListAdapter(this)
recyclerView.adapter = mAdapter
}
private fun fetchdata(url: String) {
generalBar.visibility = View.VISIBLE
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
{
val newsJsonArray = it.getJSONArray("articles")
val newsArray = ArrayList<News>()
for (i in 0 until newsJsonArray.length()) {
val newsJsonObject = newsJsonArray.getJSONObject(i)
val news = News(
newsJsonObject.getString("title"),
newsJsonObject.getString("author"),
newsJsonObject.getString("url"),
newsJsonObject.getString("urlToImage")
)
newsArray.add(news)
}
mAdapter.updateNews(newsArray)
generalBar.visibility = View.GONE
},
{
Toast.makeText(activity, "Something went wrong", Toast.LENGTH_LONG).show()
generalBar.visibility = View.GONE
}
)
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
}
fun onItemClicked(item: News) {
val builder = CustomTabsIntent.Builder()
val customTabsIntent = builder.build()
activity?.let { customTabsIntent.launchUrl(it, Uri.parse(item.url)) }
}
}
这是xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.GeneralFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
<ProgressBar
android:id="@+id/generalBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
似乎在调用generalBar
之前没有初始化。fetchdata
你需要初始化它
val generalBar = view.findViewById<ProgressBar>(R.id.generalBar)
推荐阅读
- bash - 用于 wget 以特定字符开头的 url 的 Bash 脚本
- c++ - constexpr if else 表达式中的“预期语句”
- delphi - 使用 BASS dll 和 Delphi 创建 flac 文件或 flac 流
- stata - 幂二均值:均值必须包含数字
- git - mac上的git-svn无法连接到远程svn仓库,而simple svn可以
- php - LOAD DATA INFILE 父子表和外键关系
- create-react-app - 如何通过 create-react-app 在远程开发盒上使用 npm start
- r - 将列表中的多个 data.tables 组合成两个 data.tables 的列表
- bit-framework - 如何在配置 json 文件中嵌套配置并使用位读取它?
- .net - list.Clear() 与 new List<>() - ASP.NET Core - Entity Framework Core - 延迟加载