java - Null Pointer Exception when using CustomList Adapter -
i trying create list view inside fragment. array causing null pointer exception being taken separate class called "getdata".
to create list view using custom list adapter. when put array custom list adapter error.
fragment error evolves:
package com.example.testapp; import android.os.bundle; public class fragmenta extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_a, container, false); listview listview = (listview)v.findviewbyid(r.id.list); integer[] imageid = { r.drawable.ic_launcher, r.drawable.ic_launcher, }; getdata data = getdata.getmydata(); customlist adapter = new customlist(getactivity(), data.myarray, imageid); //this put array. listview.setadapter(adapter); return v; } }
getdata class (where array taken from):
package com.example.testapp; import java.io.bufferedreader; public class getdata{ private static getdata _instance; public string myarray[]; //array set public static getdata getmydata() //this fragment calls array. { if(_instance == null) _instance = new getdata(); return _instance; } public void rundata(){ getdata data = getdata.getmydata(); data.myarray[0] = "test"; //array given value } }
you not setting array declaring it.
public string myarray[];
the below line give np because array object not initialized yet.
data.myarray[0] = "test";
you can create array object this.
public string myarray[] = new string[10];
here updated getdata class, not 100% encapsulated , singleton work. learn more encapsulating data , singleton.
package com.example.testapp; import java.io.bufferedreader; public class getdata{ private static getdata _instance; public string myarray[] = new string[10]; //array set public static getdata getmydata() //this fragment calls array. { if(_instance == null) _instance = new getdata(); _instance.rundata(); return _instance; } private void rundata(){ this.myarray[0] = "test"; //array given value } }
Comments
Post a Comment