The 'findViewById' is in color red and cannot resolve method. What is wrong with my codes and what should I do?
This is the codes for my design in charts
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6f6f6"
tools:context=".Charts">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/montserrat"
android:text="Distribution Chart"
android:textColor="#242a2c"
android:textSize="24dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/piechart"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="13dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView11">
</com.github.mikephil.charting.charts.PieChart>
</android.support.constraint.ConstraintLayout>
This is my codes for the java file of my chart fragment
package com.example.admin.test2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import java.util.ArrayList;
import java.util.List;
public class Charts extends Fragment {
float dataNum[] = {1000f, 500f, 150f, 50f, 6000f, 450f};
String dataNames[] = {"Sample Data", "Sample Data", "Sample Data", "Sample Data", "Sample Data", "Sample Data"};
public Charts() {
setupPieChart();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate (R.layout.fragment_charts, container, false);
}
private void setupPieChart()
{
//Populating a list of PieEntries;
List<PieEntry> pieEntries = new ArrayList<> ();
for (int i = 0; i < dataNum.length; i++)
{
pieEntries.add(new PieEntry(dataNum[i], dataNames[i]));
}
PieDataSet dataSet = new PieDataSet (pieEntries, "Sample Data");
PieData data = new PieData (dataSet);
//get the chart;
PieChart chartt = (PieChart) findViewById(R.id.piechart);
chartt.setData(data);
chartt.invalidate();
}
}
And this is the ERROR that I encountered
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.test2, PID: 12568
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin.test2/com.example.admin.test2.ScreenOne}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.github.mikephil.charting.charts.PieChart.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2841)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.github.mikephil.charting.charts.PieChart.findViewById(int)' on a null object reference
at com.example.admin.test2.Charts.setupPieChart(Charts.java:49)
at com.example.admin.test2.Charts.<init>(Charts.java:24)
at com.example.admin.test2.ScreenOne.<init>(ScreenOne.java:32)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1180)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
I just want to display in the chart the data in the array.