我喜欢尝试计算Android代码,但是当我在ItemSelection侦听器中初始化Cal1()方法时,请告诉我解决方案


public class MainActivity extends AppCompatActivity {

    Spinner s1,s2;
    TextView t1,t2,t3;
    String ops1,ops2;
    String[] op1={"Inclusive","+3%","+5%","+12%","+18%","+28%"};
    String[] op2={"Exclusive","-3%","-5%","-12%","-18%","-28%"};
ArrayAdapter<String> adapter,adapter2;
EditText e1,et1,et2,et3;
Button b1;
double a,b,c,x;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        s1 = (Spinner) findViewById(R.id.s1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, op1);
        s1.setAdapter(adapter);

        s2 = (Spinner) findViewById(R.id.s2);
        adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, op2);
        s2.setAdapter(adapter2);
        t1=(TextView)findViewById(R.id.tc);
        t2=(TextView)findViewById(R.id.ts);
        t3=(TextView)findViewById(R.id.ti);

        e1=(EditText)findViewById(R.id.e1) ;
        et1=(EditText)findViewById(R.id.et1);
        et2=(EditText)findViewById(R.id.et2);
        et3=(EditText)findViewById(R.id.et3);


        b1=(Button)findViewById(R.id.bt1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t1.setText("CGST");
                t2.setText("SGST");
                t3.setText("IGST");
            }
        });
      s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              switch (position)
              {
                  case 0:
                      t1.setText("CGST(1.5%)");
                      t2.setText("SGST(1.5%)");
                      t3.setText("IGST(3%)");
                      cal1(); //error pop due to this
                      break;
                  case 1:
                      t1.setText("CGST(2.5%)");
                      t2.setText("SGST(2.5%)");
                      t3.setText("IGST(5%)");
                      break;
                  case 2:
                      t1.setText("CGST(6%)");
                      t2.setText("SGST(6%)");
                      t3.setText("IGST(12%)");
                      break;
                  case 3:
                      t1.setText("CGST(9%)");
                      t2.setText("SGST(9%)");
                      t3.setText("IGST(18%)");
                      break;
                  case 4:
                      t1.setText("CGST(14%)");
                      t2.setText("SGST(14%)");
                      t3.setText("IGST(28%)");
                      break;
              }

          }
          @Override
          public void onNothingSelected(AdapterView<?> parent) {
          }
      });

      s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              switch (position)
              {
                  case 0:
                      t1.setText("CGST(-1.5%)");
                      t2.setText("SGST(-1.5%)");
                      t3.setText("IGST(-3%)");
                      break;
                  case 1:
                      t1.setText("CGST(-2.5%)");
                      t2.setText("SGST(-2.5%)");
                      t3.setText("IGST(-5%)");
                      break;
                  case 2:
                      t1.setText("CGST(-6%)");
                      t2.setText("SGST(-6%)");
                      t3.setText("IGST(-12%)");
                      break;
                  case 3:
                      t1.setText("CGST(-9%)");
                      t2.setText("SGST(-9%)");
                      t3.setText("IGST(-18%)");
                      break;
                  case 4:
                      t1.setText("CGST(-14%)");
                      t2.setText("SGST(-14%)");
                      t3.setText("IGST(-28%)");
                      break;
              }
          }
          @Override
          public void onNothingSelected(AdapterView<?> parent) {
          }
      });
    }
    private void cal1()
    {
        x=Double.parseDouble(e1.getText().toString()); //from here
    }

}

请查看//错误,例如它崩溃了应用程序我喜欢尝试计算Android代码,但是当我在项目选择侦听器中初始化Cal1()方法时:0崩溃,请告诉我解决方案获得解决方案后,请给出一定的建议使用其他方法后,它再次崩溃

->在您的代码中您的数组的索引0。

 s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              switch (position)
              {
                  case 1:
                      t1.setText("CGST(1.5%)");
                      t2.setText("SGST(1.5%)");
                      t3.setText("IGST(3%)");
                      cal1(); //error pop due to this
                      break;
                  case 2:
                      t1.setText("CGST(2.5%)");
                      t2.setText("SGST(2.5%)");
                      t3.setText("IGST(5%)");
                      break;
                  case 3:
                      t1.setText("CGST(6%)");
                      t2.setText("SGST(6%)");
                      t3.setText("IGST(12%)");
                      break;
                  case 4:
                      t1.setText("CGST(9%)");
                      t2.setText("SGST(9%)");
                      t3.setText("IGST(18%)");
                      break;
                  case 5:
                      t1.setText("CGST(14%)");
                      t2.setText("SGST(14%)");
                      t3.setText("IGST(28%)");
                      break;
              }

          }
          @Override
          public void onNothingSelected(AdapterView<?> parent) {
          }
      });

      s2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              switch (position)
              {
                  case 1:
                      t1.setText("CGST(-1.5%)");
                      t2.setText("SGST(-1.5%)");
                      t3.setText("IGST(-3%)");
                      break;
                  case 2:
                      t1.setText("CGST(-2.5%)");
                      t2.setText("SGST(-2.5%)");
                      t3.setText("IGST(-5%)");
                      break;
                  case 3:
                      t1.setText("CGST(-6%)");
                      t2.setText("SGST(-6%)");
                      t3.setText("IGST(-12%)");
                      break;
                  case 4:
                      t1.setText("CGST(-9%)");
                      t2.setText("SGST(-9%)");
                      t3.setText("IGST(-18%)");
                      break;
                  case 5:
                      t1.setText("CGST(-14%)");
                      t2.setText("SGST(-14%)");
                      t3.setText("IGST(-28%)");
                      break;
              }
          }
          @Override
          public void onNothingSelected(AdapterView<?> parent) {
          }
      });

->始终尝试调试您的代码并找出错误。这将有助于轻松找出您的错误。

相关内容

最新更新