Java MPAndroidChart未在片段中显示
我正在android studio中使用图表库。这些图表在活动中显示得很好,但我无法在片段中使用它们。尝试使用不同的图表,但我无法解决问题。我收到一条消息“没有可用的图表数据”,即使我正在向图表提供数据。基本上我有一个叫做ExamPerformanceActivity的活动(带底部导航栏),它有三个片段MathFragment,PhysicsFragment,ChemistryFragment。我在MathsFragment中有一个条形图,我面临着这个问题 活动考试成绩.xmlJava MPAndroidChart未在片段中显示,java,android,android-fragments,charts,mpandroidchart,Java,Android,Android Fragments,Charts,Mpandroidchart,我正在android studio中使用图表库。这些图表在活动中显示得很好,但我无法在片段中使用它们。尝试使用不同的图表,但我无法解决问题。我收到一条消息“没有可用的图表数据”,即使我正在向图表提供数据。基本上我有一个叫做ExamPerformanceActivity的活动(带底部导航栏),它有三个片段MathFragment,PhysicsFragment,ChemistryFragment。我在MathsFragment中有一个条形图,我面临着这个问题 活动考试成绩.xml <?xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
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.peepstake.vijay.statlog.ExamPerfoActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:animateLayoutChanges="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemIconTint="#d5d6d9"
app:itemTextColor="#ffffff"
app:menu="@menu/bottombar_student_items"/>
</RelativeLayout>
fragment_math.xml
<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="com.peepstake.vijay.statlog.Common.MathsFragment">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.github.mikephil.charting.charts.BarChart>
</FrameLayout>
MathsFragment.java
public class ExamPerformanceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam_performance);
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.maths:
selectedFragment = MathsFragment.newInstance();
break;
case R.id.physics:
selectedFragment = PhysicsFragment.newInstance();
break;
case R.id.chemistry:
selectedFragment = ChemistryFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
//Manually displaying the first fragment - one time only
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, MathsFragment.newInstance());
transaction.commit();
//Used to select an item programmatically
//bottomNavigationView.getMenu().getItem(2).setChecked(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void goTopicwise(View v){
Intent intent = new Intent(ExamPerformanceActivity.this, TopicwiseActivity.class);
startActivity(intent);
}
}
public class MathsFragment extends Fragment {
BarChart bar;
public static MathsFragment newInstance() {
MathsFragment fragment = new MathsFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public MathsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_maths, container, false);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/ProductSans.ttf");
bar = (BarChart)view.findViewById(R.id.bar);
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 100f,"Total"));
entries.add(new BarEntry(1f, 82f,"Obtained"));
entries.add(new BarEntry(2f, 95f,"Highest"));
entries.add(new BarEntry(3f, 69f,"Average"));
BarDataSet bSet = new BarDataSet(entries, "Marks");
bSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
ArrayList<String> barFactors = new ArrayList<>();
barFactors.add("Total");
barFactors.add("Obtained");
barFactors.add("Highest");
barFactors.add("Average");
XAxis xAxis = bar.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
BarData data = new BarData(bSet);
data.setBarWidth(0.9f); // set custom bar width
data.setValueTextSize(12);
Description description = new Description();
description.setTextColor(R.color.colorPrimary);
description.setText("All values in marks");
bar.setDescription(description);
bar.setData(data);
bar.setFitBars(true); // make the x-axis fit exactly all bars
bar.invalidate(); // refresh
bar.getXAxis().setValueFormatter(new IndexAxisValueFormatter(barFactors));
Legend l = bar.getLegend();
l.setFormSize(10f); // set the size of the legend forms/shapes
l.setForm(Legend.LegendForm.CIRCLE); // set what type of form/shape should be used
l.setTypeface(font);
l.setTextSize(12f);
l.setTextColor(Color.BLACK);
List<LegendEntry> lentries = new ArrayList<>();
for (int i = 0; i < barFactors.size(); i++) {
LegendEntry entry = new LegendEntry();
entry.formColor = ColorTemplate.VORDIPLOM_COLORS[i];
entry.label = barFactors.get(i);
lentries.add(entry);
}
l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
l.setYEntrySpace(5f);
l.setCustom(lentries);
return inflater.inflate(R.layout.fragment_maths, container, false);
}
}
公共类MathsFragment扩展了片段{
条形图;
公共静态MathsFragment newInstance(){
MathsFragment片段=新的MathsFragment();
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
公共数学片段(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u数学,容器,错误);
Typeface font=Typeface.createFromAsset(getActivity().getAssets(),“font/ProductSans.ttf”);
bar=(条形图)view.findViewById(R.id.bar);
列表项=新的ArrayList();
添加(新巴伦特里(0层,100层,“总计”);
增加(新巴伦特里(1f,82f,“获得”);
新增(新巴伦特里(2f,95f,“最高”);
增加(新巴伦特里(3f,69f,“平均”);
BarDataSet bSet=新的BarDataSet(条目,“标记”);
b设置设置颜色(ColorTemplate.VORDIPLOM_颜色);
ArrayList barFactors=新的ArrayList();
加上(“总计”);
添加(“获得”);
加上(“最高”);
加上(“平均值”);
XAxis XAxis=bar.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(真);
BarData数据=新的BarData(bSet);
data.setBarWidth(0.9f);//设置自定义条宽度
data.setValueTextSize(12);
Description Description=新的Description();
description.setTextColor(R.color.colorPrimary);
description.setText(“标记中的所有值”);
bar.setDescription(描述);
设置数据(数据);
bar.setFitBars(true);//使x轴正好适合所有的条
bar.invalidate();//刷新
bar.getXAxis().setValueFormatter(新的IndexaxiValueFormatter(barFactors));
图例l=bar.getLegend();
l、 setFormSize(10f);//设置图例表单/形状的大小
l、 setForm(Legend.LegendForm.CIRCLE);//设置应使用的形式/形状类型
l、 设置字体(字体);
l、 setTextSize(12f);
l、 setTextColor(颜色为黑色);
List lentrys=new ArrayList();
对于(int i=0;i
将
onCreateView
方法的最后一行更改为以下内容:
l.setCustom(lentries);
//return inflater.inflate(R.layout.fragment_maths, container, false);
return view;
图表现在将正确显示:
为什么??该方法的第一行:
View view = inflater.inflate(R.layout.fragment_maths, container, false);
膨胀视图
对象,该对象是该片段的contentView
然后您可以拨打:
bar = (BarChart)view.findViewById(R.id.bar);
在同一视图对象上获取条形图上的句柄,然后向其中添加数据
但是,在方法末尾的原始代码中,您膨胀了一个新的contentView,从而丢弃了添加了数据的视图和条形图。换句话说,原始方法设置图表,然后丢弃它,用空的条形图替换它,这是膨胀视图的简单结果。将onCreateView
方法的最后一行更改为以下内容:
l.setCustom(lentries);
//return inflater.inflate(R.layout.fragment_maths, container, false);
return view;
图表现在将正确显示:
为什么??该方法的第一行:
View view = inflater.inflate(R.layout.fragment_maths, container, false);
膨胀视图
对象,该对象是该片段的contentView
然后您可以拨打:
bar = (BarChart)view.findViewById(R.id.bar);
在同一视图对象上获取条形图上的句柄,然后向其中添加数据
但是,在方法末尾的原始代码中,您膨胀了一个新的contentView,从而丢弃了添加了数据的视图和条形图。换句话说,原始方法设置图表,然后丢弃它,用空的条形图替换它,这是膨胀视图的简单结果。谢谢!你真是帮了大忙。我非常感谢。它起作用了。@David Rawson当我点击图表时它不起作用了,然后它显示(组集图表)如何解决这个问题..?您好@Vijar SR您有没有注意到使用mpAndroidChart库和片段使项目使用AndroidX库?谢谢!你真是帮了大忙。我非常感谢。它起作用了。@David Rawson当我点击图表时它不起作用了,然后它显示(组集图表)如何解决这个问题..?您好@Vijar SR您有没有注意到使用mpAndroidChart库和片段使project使用AndroidX库?