Android Studio Spinner导致应用程序在打开之前崩溃

时间:2017-08-01 12:33:17

标签: java android

dropbox link on the project folder

每当我在android studio模拟器中运行应用程序时,它会自动崩溃。任何人都可以帮我解决我在这里做的错误吗?这里只是特定的微调器代码。但该链接有应用程序。

List < String > empSale = new ArrayList < String > ();
empSale.add("Sale Items:");
empSale.add("Apple iPhone 8");
empSale.add("Samsung Galaxy 9");
empSale.add("Asus Zenfone 10");
empSale.add("Nokia 3310");

spnSale = (Spinner) findViewById(R.id.spnSale);
spnSale.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);

//Spinner data
aA = new ArrayAdapter < String > (this, android.R.layout.simple_spinner_dropdown_item, empSale);
aA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSale.setAdapter(aA);

spnSale.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {

    String items = parent.getItemAtPosition(position).toString();

    Toast.makeText(parent.getContext(), "Selected " + items, Toast.LENGTH_SHORT).show();

    if (items.equals("Sale Items:")) {
      //price = 0;
      txtPrice.setText("Price: Php 0.00");
    }
    if (items.equals("Apple iPhone 8")) {
      //price = 60000;
      txtPrice.setText("Price: Php 60000.00");
    }
    if (items.equals("Samsung Galaxy 9")) {
      //price = 55000;
      txtPrice.setText("Price: Php 55000.00");
    }
    if (items.equals("Asus Zenfone 10")) {
      //price = 50000;
      txtPrice.setText("Price: Php 50000.00");
    }
    if (items.equals("Nokia 3310")) {
      //price = 1000000;
      txtPrice.setText("Price: Php 1000000.00");
    }
  }
});

2 个答案:

答案 0 :(得分:0)

没关系,我所要做的就是删除aA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

下面的aA = new ArrayAdapter < String > (this, android.R.layout.simple_spinner_dropdown_item, empSale);

答案 1 :(得分:0)

简单 只需删除项目点击侦听器上的微调器,因为onItemClick侦听器不适用于Spinners。

       spnSale = (Spinner) findViewById(R.id.spnSale);

       spnSale.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);

     //Spinner data
     aA = new ArrayAdapter < String > (this, 
     android.R.layout.simple_spinner_dropdown_item, empSale);


   aA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spnSale.setAdapter(aA);


   String item = spnSale.getItemSelected().toString();
if (items.equals("Sale Items:")) {
  //price = 0;
  txtPrice.setText("Price: Php 0.00");
}else
if (items.equals("Apple iPhone 8")) {
  //price = 60000;
  txtPrice.setText("Price: Php 60000.00");
}else
if (items.equals("Samsung Galaxy 9")) {
  //price = 55000;
  txtPrice.setText("Price: Php 55000.00");
}else
if (items.equals("Asus Zenfone 10")) {
  //price = 50000;
  txtPrice.setText("Price: Php 50000.00");
}else
if (items.equals("Nokia 3310")) {
  //price = 1000000;
  txtPrice.setText("Price: Php 1000000.00");
}
  ....
  ....

试试这个