我试图用我的数组服务填充listView,数组服务填充了"概念"和" priceDo"从EditText获取的变量,它被初始化的服务数组。我已经在stackOverflow和Google搜索了这里,但我找不到有用的东西。 当我编译并运行应用程序时,它会抛出空指针 在这一行:
mainListView.setAdapter(my);
这是我的代码:
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);
}
}