gpt4 book ai didi

android - 饼图未显示在 fragment 中(Android)

转载 作者:行者123 更新时间:2023-11-29 19:10:17 24 4
gpt4 key购买 nike

我正在开发一个 andorid 应用程序,在这个应用程序中,我试图在一个带有选项卡的 fragment 中显示一个饼图。但它显示“没有可用的图表数据”。当我尝试在一项 Activity 中做同样的事情时,它表现得很完美。我正在使用“MPAndroidChart”作为引用。请帮忙。提前致谢!

这是我的 fragment 类

import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;


import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;


import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;






public class overview extends android.support.v4.app.Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static View rootView;
private OnFragmentInteractionListener mListener;
private SeekBar mSeekBarX, mSeekBarY;
private TextView tvX, tvY;
private PieChart mChart;

private Typeface tf;



public overview() {
// Required empty public constructor
}

//String param1, String param2
public static overview newInstance() {
overview fragment = new overview();
Bundle args = new Bundle();
// args.putString(ARG_PARAM1, param1);
//args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getArguments() != null) {

}
}
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.overview, container, false);

PieChart pieChart = (PieChart) findViewById(R.id.chart);

ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(4f, 0));
entries.add(new Entry(8f, 1));
entries.add(new Entry(6f, 2));
entries.add(new Entry(12f, 3));
entries.add(new Entry(18f, 4));
entries.add(new Entry(9f, 5));

PieDataSet dataset = new PieDataSet(entries, "# of Calls");

ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");

PieData data = new PieData(labels, dataset);
dataset.setColors(ColorTemplate.COLORFUL_COLORS); //
pieChart.setDescription("Description");
pieChart.setData(data);

pieChart.animateY(5000);

pieChart.saveToGallery("/sd/mychart.jpg", 85); // 85 is the quality of
the image




if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.overview, container, false);

} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return rootView;
}

private SpannableString generateCenterText() {
SpannableString s = new SpannableString("Revenues\nQuarters 2015");
s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0);
s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0);
return s;
}


@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

}

这是我的 xml 文件:

     <com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="300dp"
android:layout_height="300dp" />

最佳答案

其实你对PiChart的声明是错误的,请这样正确声明;

 View view = inflater.inflate(R.layout.overview, container, false);

PieChart pieChart = (PieChart)view.findViewById(R.id.chart);

关于android - 饼图未显示在 fragment 中(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45605733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com