简体   繁体   中英

Mathematical formula with variables and spinner

I hope someone can help me? I'm trying to solve a problem I have with getting a certain number into a formula.

I've made some Changes but I can still not get it to work. It crashes when I try to open it. Anyone know why? From the Logcat I can see that the problem is in OnCreate.

public class ActivityEnergy extends Activity implements OnClickListener {

    EditText etNum10;
    EditText etNum20;
    EditText etNum30;
    int nrjspinner = 0;
    Object value;

    Button btnNrj;

    String oper = "";

    TextView tvResult;

 ArrayList<String> nrjmethod = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.energy);

     // find the elements
    etNum10 = (EditText) findViewById(R.id.etNum10);
    etNum20 = (EditText) findViewById(R.id.etNum20);
    etNum30 = (EditText) findViewById(R.id.etNum30);

        btnNrj = (Button) findViewById(R.id.btnNrj);

        tvResult = (TextView) findViewById(R.id.tvResult);

        // set a listener
        btnNrj.setOnClickListener(this);

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

  // Create the ArrayAdapter
  ArrayAdapter <CharSequence> adapter = ArrayAdapter.createFromResource( this,
          R.array.nrjmethod, android.R.layout.simple_spinner_item);

                 // Set the Adapter
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(adapter);


  // Set the ClickListener for Spinner
  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

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

          value = parent.getItemAtPosition(position);
          switch (position) {
          case 0:
              nrjspinner = 1;
              break;

          case 1:
              nrjspinner = 0.8;
              break;

          case 2:
              nrjspinner = 0.8;
              break;

          case 3:
              nrjspinner = 0.8;
              break;

          case 4:
              nrjspinner = 0.8;
              break;

          case 5:
              nrjspinner = 0.8;
              break;

          case 6:
              nrjspinner = 0.8;
              break;

          case 7:
              nrjspinner = 0.8;
              break;

          case 8:
              nrjspinner = 0.8;
              break;

          case 9:
              nrjspinner = 0.8;
              break;

          case 10:
              nrjspinner = 0.6;
              break;

          case 11:
              nrjspinner = 0.6;
              break;
          }

      }


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



    } 

        });


 }

    @Override
         public void onClick(View v) {
                // TODO Auto-generated method stub
                float num10 = 0;
                float num20 = 0;
                float num30 = 0;
                float result = 0;

                // check if the fields are empty
                if (TextUtils.isEmpty(etNum10.getText().toString())
                    || TextUtils.isEmpty(etNum20.getText().toString())
                    || TextUtils.isEmpty(etNum30.getText().toString())) {
                  return;
                }

                // read EditText and fill variables with numbers
                num10 = Float.parseFloat(etNum10.getText().toString());
                num20 = Float.parseFloat(etNum20.getText().toString());
                num30 = Float.parseFloat(etNum30.getText().toString());

             // defines the button that has been clicked and performs the corresponding operation
               switch (v.getId()) {
                case R.id.btnNrj:
                  oper = "";
                  result = (float) ((nrjspinner * ((num10 * num20) / num30))*0.001);
                  break;
                default:
                  break;

                }

             // form the output line
                tvResult.setText("Your result = " + result);

    }


}

And here's the logcat

10-27 17:40:58.832: E/AndroidRuntime(1497): FATAL EXCEPTION: main

10-27 17:40:58.832: E/AndroidRuntime(1497): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.develope.axson/se.develope.axson.ActivityEnergy}: java.lang.NullPointerException

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.access$600(ActivityThread.java:141)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.os.Handler.dispatchMessage(Handler.java:99)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.os.Looper.loop(Looper.java:137)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.main(ActivityThread.java:5103)

10-27 17:40:58.832: E/AndroidRuntime(1497): at java.lang.reflect.Method.invokeNative(Native Method)

10-27 17:40:58.832: E/AndroidRuntime(1497): at java.lang.reflect.Method.invoke(Method.java:525)

10-27 17:40:58.832: E/AndroidRuntime(1497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

10-27 17:40:58.832: E/AndroidRuntime(1497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

10-27 17:40:58.832: E/AndroidRuntime(1497): at dalvik.system.NativeStart.main(Native Method)

10-27 17:40:58.832: E/AndroidRuntime(1497): Caused by: java.lang.NullPointerException

10-27 17:40:58.832: E/AndroidRuntime(1497): at se.develope.axson.ActivityEnergy.onCreate(ActivityEnergy.java:52)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.Activity.performCreate(Activity.java:5133)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

10-27 17:40:58.832: E/AndroidRuntime(1497): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

10-27 17:40:58.832: E/AndroidRuntime(1497): ... 11 more

Try debugging and check getting value from spinner. May be you can not get the value from spinner. In such cases, the best solution is debugging.

Problem solved! Finally!

First of all the biggest problem was the SetOnClickListener(THIS); It made everything crash. I got rid of the Listener-function and replaced it with the following solution:

In the XML-file - add android:onClick="ButtonOnClick"

For example:

    <Button
    android:onClick="ButtonOnClick" 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:text="@string/calcNrjbtn"
    android:textSize="8pt" 
    android:id="@+id/btnCalcNrj">

And in the java - Replace OnClick with your defined OnClick

For example:

public void ButtonOnClick(View v) {
        // TODO Auto-generated method stub
        double num10 = 0;
        double num20 = 0;
        ....

It was Chronos who gave me that solution in the following thread: Android OnClickListener - identify a button

Since I was using decimals, INT didn't work so I changed to DOUBLE instead and everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM