I'm trying to fill a listView with my array service, the array service is fill with the "concept" and "priceDo" variables taken from an EditText, The service array it is initialized. I've searched here in stackOverflow and Google but I can't find something helpfull. When I compile and run the application it throws me the null pointer in this line:
mainListView.setAdapter(my);
Here's my code:
public class ConceptsActivity extends AppCompatActivity{
private int i=0;
private AutoCompleteTextView actv;
private TextView tvConcept;
private TextView tvConcept2;
private TextView warning;
private TextView tvPrice;
private EditText price;
private Button addConcept;
private String concept;
private double priceDo;
private List<AddConceptsListView> myConcept = new ArrayList<AddConceptsListView>();
private Service[] service = new Service[100];
private ListView mainListView ;
public ConceptsActivity(){
}
public ConceptsActivity(Service service[]){
this.service = service;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle(R.string.TitleSer);
super.onCreate(savedInstanceState);
setContentView(R.layout.concepts_layout);
for(int j=0; j<service.length; j++){
service[j] = new Service();
if(service[j].getName() != null){
i++;
}
}
mainListView = (ListView) findViewById( R.id.list2 );
actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewConcept);
tvConcept = (TextView) findViewById(R.id.textViewConcept);
tvConcept2 = (TextView) findViewById(R.id.textViewConcept2);
warning = (TextView) findViewById(R.id.textViewWarning);
tvPrice = (TextView) findViewById(R.id.textViewPrice);
price = (EditText) findViewById(R.id.editTextPrice);
addConcept = (Button) findViewById(R.id.buttonAddConcept);
//Autocomplete Textview
String[] services = getResources().getStringArray(R.array.services_array);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, services);
actv.setAdapter(adapter);
actv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
//make items visible
tvConcept.setVisibility(View.VISIBLE);
tvConcept2.setVisibility(View.VISIBLE);
tvPrice.setVisibility(View.VISIBLE);
price.setVisibility(View.VISIBLE);
addConcept.setVisibility(View.VISIBLE);
concept = actv.getText()+"";
tvConcept2.setText(concept);
//To hide the keyboard
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
handled = true;
}
return handled;
}
});
}
public void onClickAddConcept(View view){
if(price.getText().toString().equals("")){
warning.setVisibility(View.VISIBLE);
}
else {
warning.setVisibility(View.INVISIBLE);
priceDo = Double.parseDouble(price.getText().toString());
Toast.makeText(getApplicationContext(),"Precio: " + priceDo+ "" + "\n"
+ "Concepto: " + concept,Toast.LENGTH_SHORT).show();
service[i].setName(concept);
service[i].setPrice(priceDo);
i++;
for(int j=0; j<service.length; j++){
if(service[j].getName() != null || service[j].getPrice() != null){
Toast.makeText(getApplicationContext(), "Array" + j + "nameConcept: " + service[j].getName() +
"\n" + "Array price: " + service[j].getPrice(), Toast.LENGTH_SHORT).show();
}
}
}
ArrayAdapter<Service> my = new ArrayAdapter<Service>(this, android.R.layout.simple_list_item_1, service);
mainListView.setAdapter(my);
}
}