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


i create demo of activity ok, when move fragment, there

error here

java.lang.nullpointerexception: attempt invoke virtual method 'void      com.hrules.charter.charterxlabels.setstickyedges(boolean)' on null object  reference @ com.hb.bottomlast.persion2_fragment.oncreateview(persion2_fragment.java:73)` 

the code here

/**  * created 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];         (int = 0; < 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];         (int = 0; < 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 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"     /> </linearlayout> 

here setstickyedges

  public void setstickyedges(boolean stickyedges) {     this.stickyedges = stickyedges;     invalidate();   } 

from error log, seems charterlinelabelx null, maybe there mistakes usage of butterknife.

i saw use butterknife.bind(view) in fragment, incorrect.

you should replace with:

butterknife.bind(this, view) 

for more information, check this


Comments