添加变量MainActivity Android java.util.NoSuchElementException时出错:

时间:2017-02-10 18:42:44

标签: android

我在Android Studio

类的MainActiviy类中添加变量时遇到问题
  

public int quantity = 2;

它会抛出错误

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{se.kerio.justjava/se.kerio.justjava.MainActivity}: java.lang.RuntimeException: java.util.NoSuchElementException: quantity

我想要做的是使用一些按钮减少或增加textView的值

我是这个领域的新手,我试图在两个方法中使用这个变量,目前我没有使用submitOrder方法,只是增量和减量。

谢谢,任何建议都会有所帮助:)

MainActivity.java

package se.kerio.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    public int quantity = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int quantity = 3;
        display(quantity);
        displayPrice(quantity*5);
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }
    /*
    * This method increments the quantity
     */
    public void increment(View view){
        quantity = quantity + 1;
        display(quantity);
    }

    /*
    * This method decrements the quantity
     */
    public void decrement(View view){
        quantity = quantity - 1;
        display(quantity);
    }
}

I want to change the value 2 with the buttons

0 个答案:

没有答案