我想在右侧导航抽屉中使用图表图表。我从服务器获取值。服务器响应没问题。问题是我得到一个NullPointerException。我从我的主类中调用 setMonthMatGroupSummery()方法。
这是导航抽屉类。
public class MonthItemSummeryNavigationDrawerFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private View containerView;
private DrawerLayout mDrawerlayout;
private ActionBarDrawerToggle mDrawerToggle;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
public static final String PREF_FILE_NAME = "testpref";
public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";
private String systemDate, oriSystemDate;
private DateFormat df1, df2;
private ArrayList<String> mGroupAchList;
private float f;
private HorizontalBarChart chart;
private BarData barData;
private ArrayList<String> aaa;
public MonthItemSummeryNavigationDrawerFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));
if (savedInstanceState != null) {
mFromSavedInstanceState = true;
}
}
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View vieww = inflater.inflate(R.layout.matgroup_summery_navigation_drawer, container, false);
chart = (HorizontalBarChart) vieww.findViewById(R.id.hchart);
df1 = new SimpleDateFormat("yyyy");
systemDate = df1.format(new Date());
return vieww;
}
public void setMonthMatGroupSummery(ArrayList<String> r) {
this.mGroupAchList = r;
f = Float.parseFloat(mGroupAchList.get(0));
Log.d("Array List f = ", String.valueOf(f));
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("FG0028");
xAxis.add("FG0015");
xAxis.add("FG0010");
xAxis.add("FG0009");
xAxis.add("FG0005");
xAxis.add("FG0004");
xAxis.add("FG0002");
xAxis.add("FG0001");
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
Log.d("Array List test = ", String.valueOf(f));
BarEntry v1e1 = new BarEntry(110.000f, 0); // FG0028
valueSet1.add(v1e1);
BarEntry v1e2 = new BarEntry(40.000f, 1); // FG0015
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(60.000f, 2); // FG0010
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(30.000f, 3); // FG0009
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(90.000f, 4); // FG0005
valueSet1.add(v1e5);
BarEntry v1e6 = new BarEntry(100.000f, 5); // FG0004
valueSet1.add(v1e6);
BarEntry v1e7 = new BarEntry(90.000f, 6); // FG0002
valueSet1.add(v1e7);
BarEntry v1e8 = new BarEntry(80.000f, 7); // FG0001
valueSet1.add(v1e8);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Ton");
barDataSet1.setColor(Color.rgb(0, 155, 0));
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
barData = new BarData(xAxis, dataSets);
chart.animateXY(4000, 4000);
chart.invalidate();
chart.setPinchZoom(false);
chart.setTouchEnabled(false);
chart.setDescription("");
chart.setData(barData);
XAxis xAxiss = chart.getXAxis();
xAxiss.setDrawGridLines(false);
xAxiss.setDrawAxisLine(true);
xAxiss.setPosition(XAxis.XAxisPosition.BOTTOM);
YAxis yAxisLeft = chart.getAxisLeft();
yAxisLeft.setDrawGridLines(true);
yAxisLeft.setEnabled(false);
YAxis yAxisRight = chart.getAxisRight();
yAxisRight.setDrawGridLines(true);
yAxisRight.setEnabled(false);
}
}
}
我的 LogCat
06-10 14:51:50.277 14857-14857/com.ceatkelanisrilanka.dushanmadushanka.ceat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ceatkelanisrilanka.dushanmadushanka.ceat, PID: 14857
java.lang.NullPointerException: Attempt to invoke virtual method
'void com.github.mikephil.charting.charts.HorizontalBarChart.animateXY(int, int)' on a null object reference
at com.ceatkelanisrilanka.dushanmadushanka.ceat.fragments
.MonthItemSummeryNavigationDrawerFragment.setMonthMatGroupSummery(MonthItemSummeryNavigationDrawerFragment.java:128)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.WelcomeMenu.getMatGroupSummery(WelcomeMenu.java:740)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.WelcomeMenu$1.onResponse(WelcomeMenu.java:311)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.WelcomeMenu$1.onResponse(WelcomeMenu.java:257)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.json.CustomJsonObjectRequest.deliverResponse(CustomJsonObjectRequest.java:60)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.json.CustomJsonObjectRequest.deliverResponse(CustomJsonObjectRequest.java:17)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-10 14:56:38.185 14857-14857/com.ceatkelanisrilanka.dushanmadushanka.ceat I/Process: Sending signal. PID: 14857 SIG: 9