背景:
com.github.mikephil.charting.charts.BarChart
。 我有以下问题:
我收到以下错误消息:
barChart不能为空
我检查了Log消息,并在该消息之前获取了JSON值。错误从这段代码barChart.data = data
默认情况下不会调用默认片段一个,但是如果我单击第二个片段,然后单击第一个片段,则会调用片段1。否则,它将无法正常工作或引发上述错误。
附加代码:
JSON响应
[{"day":"2018-12-12","value":"1"},{"day":"2018-12-13","value":"2"},{"day":"2018-12-14","value":"3"},{"day":"2018-12-15","value":"4"},{"day":"2018-12-17","value":"5"},{"day":"2018-12-18","value":"6"},{"day":"2018-12-19","value":"7"}]
片段:1
class FirstFragment : Fragment() {
var volleyRequest: RequestQueue?=null
val TestLink="https://wwww.abc.app"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
volleyRequest= Volley.newRequestQueue(this.context)
getTestValue(TestLink)
}
fun getTestValue(Url:String)
{
val stringReq=StringRequest(Request.Method.GET,Url,
Listener {
response: String? ->
try {
Log.d("Response JSON",response.toString())
///Bar Chart Test
val jsonStringArray=response.toString()
println("Attempt to fetch JSON Data")
val entries = ArrayList<BarEntry>()
val labels = ArrayList<String>()
val arr = JSONArray(jsonStringArray)
for (i in 0 until arr.length()) {
entries.add(BarEntry(arr.getJSONObject(i).getDouble("value").toFloat(), i))
labels.add(arr.getJSONObject(i).getString("day"))
}
val barDataSet = BarDataSet(entries, "Test Sample Record")
barDataSet.valueTextSize = 10f
val data = BarData(labels, barDataSet)
Log.d("Entries", data.toString())
barChart.data = data
barChart.setDescription("")
}
catch (e:JSONException)
{
e.printStackTrace()
}
},
Response.ErrorListener {
error: VolleyError? ->
try{
Log.d("Error:", error.toString())
}
catch (e:JSONException) {e.printStackTrace()}
}
)
volleyRequest!!.add(stringReq)
}
}
主要活动:
class MainActivity : AppCompatActivity() {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
// message.setText(R.string.title_home)
replaceFragment(FirstFragment())
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
//message.setText(R.string.title_dashboard)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
//message.setText(R.string.title_notifications)
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
private fun replaceFragment(fragment: Fragment){
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container, fragment)
fragmentTransaction.commit()
}
}
active_fragment_one
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context="FragmentOne">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/ColorBg"
/>
</FrameLayout>
答案 0 :(得分:1)
我不知道您在onCreateView()
中的FirstFragment
,但是您的问题在尚未创建时被称为“视图”,因此将您在onCreate
中的代码移至
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
volleyRequest= Volley.newRequestQueue(this.context)
getTestValue(TestLink)
}