operator“*”不能应用于java.lang.string

时间:2018-01-08 16:14:54

标签: android

我正在尝试编写一个应用程序,用户可以通过从微调器中选择他们的地址和汽车模型来计算碳排放量。为了计算碳排放量,我必须将地址和汽车模型排放中的相应里程相乘。不幸的是,我被操作符“*”命中,最后无法应用于java.lang.string。

public class Calculator extends AppCompatActivity {

Spinner sp ;
Spinner car;
String adarray[] = {"Address1", "Address2", "Address3", "Address4"};
String cararry[] = {"Car1", "CAR2", "CAR3", "CAR4"};
ArrayAdapter <String> adressapter1;
ArrayAdapter <String> carapter;
String record1;
String record2;
TextView displayaddressdata;
TextView displaycardata;
TextView carbonemission;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);

    sp = (Spinner)findViewById(R.id.address);
    car = (Spinner)findViewById(R.id.carmodel);
    adressapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adarray);
    carapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cararry);
    displayaddressdata = (TextView)findViewById(R.id.result1);
    displaycardata = (TextView)findViewById(R.id.result2);

    sp.setAdapter(adressapter1);
    car.setAdapter(carapter);

    car.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            switch (position) {
                case 0:
                    record2 = "3";
                    break;
                case 1:
                    record2 = "6";
                    break;
                case 2:
                    record2 = "9";
                    break;

                case 3:
                    record2 = "12";
                    break;

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            switch (position)
            {
                case 0:
                   record1 = "8";
                   break;
                case 1:
                    record1 = "16";
                    break;

                case 2:
                    record1 = "24";
                    break;
                case 3:
                    record1 = "32";
                    break;

            }

        }


        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });




}

public void displayresult (View view)
{
    displayaddressdata.setTextSize(18);
    displayaddressdata.setText(record1);

    displaycardata.setTextSize(18);
    displaycardata.setText(record2);




}
public void calculate (View view)
{
carbonemission.setText(displayaddressdata.getText().toString()
        *displaycardata.getText().toString());
carbonemission.setTextSize(18);

 }

   }

1 个答案:

答案 0 :(得分:0)

此:

carbonemission.setText(displayaddressdata.getText().toString()
    *displaycardata.getText().toString());

运行时是否与:

相同
"string" * "string"

这不是允许的操作。您不能将String与String相乘。你可以用数字来做。

如果您检查的值保证是一个数字,请使用适当的数字类(如果您使用double,请转到Double类,float - &gt; Float,int - &gt; Integer,您会得到这个想法)和适当的解析方法:

carbonemission.setText(integer.parseInt(displayaddressdata.getText().toString())
    * Integer.parseInt(displaycardata.getText().toString()));

以上是使用Integer类的示例,但是parse方法在所有带有基本原语的数字类中都可用(例如float,double,long)