graph(MpAndroidChart=library, class=BarChart)
을 나타내는 View
을 업데이트하려고합니다. 나머지 호출에서 반환 된 입력을 기준으로 Fragment
내에 있습니다. updateGraph()
이라는 메서드를 사용하여 뷰를 업데이트하려고 시도하고 널 포인터 예외가 발생합니다.
내 updateGraph()
방법에 대한 코드는 다음과 같습니다.Null 포인터 예외 내에서 단편
이 문제에 대한 도움을 주시면 감사하겠습니다.
호출되는이 방법은 다음과 같다 방법에 대한 일반적인 설명 : MainActivity
내가 만들고 내 Activity
의 필드로 사용할 FragmentOne
의 인스턴스에 updateGraph()
을 호출하는 실행 가능한을 만들어 내.
그런 다음 해당 실행 파일을 내 RestCall(AsyncTask)
으로 전달하여 활동 자체를 전달하지 않도록하십시오.
나머지 호출의 정보가 반환되면 그래프를 업데이트하기 위해 SharedPreferences
에서 수행 할 것과 비슷한 단일 톤으로 저장하고 참조합니다.
아래의 코드는 그래프를 업데이트하기 위해 미리 정의 된 값을 사용하며, 지금 그래프를 얻으려고하기 때문에 방금 설명한 정보를 참조하는 것에 대해 걱정하지 않습니다.
public class FragmentOne extends Fragment implements Summary {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
@Override
public void updateGraph() {
BarChart theChart = (BarChart) this.getView().findViewById(R.id.chart_frag_one);
ArrayList<BarEntry> entries = new ArrayList<>();
List<TupleFloat> prices = new ArrayList<TupleFloat>();
prices.add(new TupleFloat(2,4));
prices.add(new TupleFloat(3,4));
prices.add(new TupleFloat(1,6));
prices.add(new TupleFloat(3,1));
prices.add(new TupleFloat(1,1));
prices.add(new TupleFloat(7,2));
prices.add(new TupleFloat(3,2));
int prices_length = prices.size(); // need to delete
for (int i = 1; i < prices_length+1; i++){
entries.add(new BarEntry((float) (i*2), new float[] {prices.get(i-1).getX(), prices.get(i-1).getY()}));
}
BarDataSet dataset = new BarDataSet(entries, " Summary"); // asbtract Summary
String datas[] = {"regular", "peak"};
dataset.setStackLabels(datas);
dataset.setColors(new int[] {R.color.portal_dark_blue, R.color.portal_light_blue}, getActivity());
YAxis left = theChart.getAxisLeft();
left.setAxisMaxValue(10);//dataset.getYMax()+2);
left.setAxisMinValue(0);
theChart.getAxisRight().setEnabled(false);
XAxis bottomAxis = theChart.getXAxis();
bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
bottomAxis.setValueFormatter(new AxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
// return values will all be the values of the dates array
int value_i = (int) value;
Log.d("hello", Integer.toString(value_i));
switch(value_i){
case 2:
return "Jul 27";
case 4:
return "Jul 28";
case 6:
return "Jul 29";
case 8:
return "Jul 30";
case 10:
return "Jul 31";
case 12:
return "Aug 1";
case 14:
return "Aug 2";
}
return "";
}
@Override
public int getDecimalDigits() {
return 0;
}
});
bottomAxis.setAxisMinValue(0);
bottomAxis.setLabelCount(16); // size of array * 2 + 1
bottomAxis.setAxisMaxValue(dataset.getXMax()+3); // going to be dates-array based
bottomAxis.setDrawGridLines(false);
theChart.setDrawValueAboveBar(false);
theChart.setDescription("");
BarData data = new BarData(dataset);
theChart.setData(data);
// legend
Legend legend = theChart.getLegend();
legend.setYOffset(40);
legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
legend.setTextSize(200);
}
}
스택 추적 :
08-09 12:59:27.312 27094-27094/com.example.schadtj.portalapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.schadtj.portalapp, PID: 27094
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.schadtj.portalapp.FragmentOne.updateGraph(FragmentOne.java:38)
at com.example.schadtj.portalapp.MainActivity$1.run(MainActivity.java:36)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Fragment_one의 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fvone"
>
<LinearLayout
android:id="@+id/tobegraph"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart_frag_one"
style="@style/AppTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
</LinearLayout>
</LinearLayout>
MainActivity 필요한 코드
public class MainActivity extends Activity{
FragmentManager FM = getFragmentManager();
private BottomBar mBottomBar;
FragmentOne frag1;
@Override
public void onCreate(Bundle savedInstanceState) {
// Map of Runnables
Map<String, Runnable> rest_runnables = new HashMap<String, Runnable>();
frag1 = new FragmentOne();
Runnable summary = new Runnable() {
@Override
public void run() {
frag1.updateGraph();
}
};
rest_runnables.put("Summary", summary);
new RestUpdateAll(this, ((MyApplication) this.getApplication()).returnUser(), rest_runnables).execute();
스택 추적 예외? –
'fragment_one'레이아웃 파일을 공유하십시오. –
실마리가 있습니다. java.lang.NullPointerException : 가상 메소드 'android.view.View android.view.View.findViewById (int)'를 null 객체에 호출하려고 시도했습니다. findViewById를 호출 한 활동 확인 - – ishmaelMakitla