按钮适用于模拟器,但在设备上崩溃

时间:2015-06-09 01:30:41

标签: java android button crash

我正在听onCreate任务上的按钮,然后通过XML调用一个函数。该按钮有一个“按钮Button02;”在公共类MainActivity上,我正在听,所有其他按钮都是这样工作的

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

    /* some shit*/

    // 2. Access the Button defined in layout XML
    // and listen for it here
    Button01 = (Button) findViewById(R.id.Button01);
    Button02 = (Button) findViewById(R.id.Button02);
    Button03 = (Button) findViewById(R.id.Button03);

    // X. Instantiate the first text
    updateQuotes(textView01);
}

我试图调用的函数是:

    public void calcGold (View view) {
    String goldStr = "", purityStr = "", weightStr = "", priceStr = "";
    double gold, purity, weight, price;

    try {
        goldStr = EditText01.getText().toString();
        purityStr = EditText02.getText().toString();
        weightStr = EditText03.getText().toString();
        priceStr = EditText04.getText().toString();
    } catch (Exception e) {
        Context context1 = getApplicationContext();
        CharSequence text1 = "Não foi possível ler as entradas";

        Toast toast = Toast.makeText(context1, text1, Toast.LENGTH_SHORT);
        toast.show();
    }

    if (goldStr.isEmpty() || "".equals(goldStr)) {
        gold = 0.0;
    } else {
        gold = Double.parseDouble(EditText01.getText().toString());
    }
    if (purityStr.isEmpty() || "".equals(purityStr)) {
        purity = 0.0;
    } else {
        purity = Double.parseDouble(EditText02.getText().toString()) / 100;
    }
    if (weightStr.isEmpty() || "".equals(weightStr)) {
        weight = 0.0;
    } else {
        weight = Double.parseDouble(EditText03.getText().toString());
    }
    if (priceStr.isEmpty() || "".equals(priceStr)) {
        price = 0.0;
    } else {
        price = Double.parseDouble(EditText04.getText().toString());
    }

    // price = gold * weight * purity
    if (gold == 0) {
        gold = price / (purity * weight);
        String tempVar = "" + gold;
        EditText01.setText(tempVar);
    } else if (purity == 0) {
        purity = price / (gold * weight);
        String tempVar = "" + (purity * 100);
        EditText02.setText(tempVar);
    } else if (weight == 0) {
        weight = price / (gold * purity);
        String tempVar = "" + weight;
        EditText03.setText(tempVar);
    } else if (price == 0){
        price = gold * weight * purity;
        String tempVar = "";
        tempVar = tempVar + price;
        EditText04.setText(tempVar);
    } else {
        Context context = getApplicationContext();
        CharSequence text = "Todas as caixas estão cheias";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

}

activity_main.xml上的按钮是:

<Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="30dp"
        android:onClick="calcGold"
        android:text="@string/calc"/>

但是,在模拟器以外的设备上按下它时会崩溃。

0 个答案:

没有答案