R Probaby R Probaby - 3 months ago 6
Android Question

i bulid a chart of activity is ok,but when i build it of fragment there some error

i creat this demo of activity is ok,when i move it to fragment,there are some

error is here


java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.hrules.charter.CharterXLabels.setStickyEdges(boolean)' on a null object reference at com.hb.bottomlast.Persion2_fragment.onCreateView(Persion2_fragment.java:73)`


the code is here


/**
* Created by uname on 2016/08/29 0029.
*/
public class Persion2_fragment extends Fragment {
@BindView(R.id.charter_line)
CharterLine charterLine;
@BindView(R.id.charter_line_XLabel)
CharterXLabels charterLineLabelX;
@BindView(R.id.charter_line_with_XLabel)
CharterLine charterLineWithLabel;
@BindView(R.id.charter_line_YLabel)
CharterYLabels charterLineYLabel;
@BindView(R.id.charter_line_with_XMarker)
CharterLine charterLineWithXMarker;
@BindView(R.id.charter_line_XMarker)
CharterXMarkers charterLineXMarkers;
@BindView(R.id.charter_line_with_YMarker)
CharterLine charterLineWithYMarker;
@BindView(R.id.charter_line_YMarker)
CharterYMarkers charterLineYMarkers;
@BindView(R.id.ychart)
CharterYLabels ychart;
private static final int DEFAULT_ITEMS_COUNT = 7;
private static final int DEFAULT_RANDOM_VALUE_MIN = 10;
private static final int DEFAULT_RANDOM_VALUE_MAX = 100;

private float[] values;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.persion4, container, false);
ButterKnife.bind(view);
Resources res = getResources();
int[] barColors = new int[]{
res.getColor(R.color.colorAccent), res.getColor(R.color.colorAccent),
res.getColor(R.color.colorAccent)
};
charterLineLabelX.setStickyEdges(true);
String[] values3 = {"a", "4", "5", "7", "6", "3", "6"};
charterLineLabelX.setValues(values3);
// String[] values2={"1","6","5","4","3","2","7"};
String[] values5 = {"1", "2", "3", "4", "5", "6", "7"};
String[] values2 = this.getRightValues(values5);
Log.d("values2", Arrays.toString(values2));
ychart.setValues(values2);
final float[] values4 = {1, 3, 3.5f, 4, 5, 6, 7};
charterLineWithLabel.setValues(values4);
charterLineWithLabel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX,
// DEFAULT_RANDOM_VALUE_MIN);
charterLineWithLabel.setValues(values4);
charterLineWithLabel.show();
ychart.setValues(values4);
charterLineLabelX.setValues(values4);
}
});
return view;
}
private float[] fillRandomValues(int length, int max, int min) {
Random random = new Random();
float[] newRandomValues = new float[length];
for (int i = 0; i < newRandomValues.length; i++) {
//newRandomValues[i] = random.nextInt(max - min + 1) - min;
newRandomValues[i]=i;
}
return newRandomValues;
}
public String[] getRightValues(String[] valuesPar) {
String[] test = new String[valuesPar.length];
for (int i = 0; i < valuesPar.length; i++) {
if (i == 0||i==(valuesPar.length-1)){
test[i] = valuesPar[i];
continue;
}
test[i]=valuesPar[valuesPar.length-1-i];
}
return test;
}
}



here is xml

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<com.hrules.charter.CharterLine
android:id="@+id/charter_line_with_XLabel"
android:layout_width="fill_parent"
android:layout_height="100dp"
/>
<com.hrules.charter.CharterXLabels
android:id="@+id/charter_line_XLabel"
android:layout_width="fill_parent"
android:layout_height="20dp"
/>




here is the setStickyEdges

` public void setStickyEdges(boolean stickyEdges) {
this.stickyEdges = stickyEdges;
invalidate();
}`

Answer

From you error log, it seems that charterLineLabelX is null, so maybe there is some mistakes about your usage of ButterKnife.

I saw you use ButterKnife.bind(View) in your fragment, this is incorrect.

You should replace it with:

ButterKnife.bind(this, view)

For more information, check this