2015-11-08 2 views
0

Приложение выполнено просто отлично, а интерфейс загружается также очень хорошо, но когда я пытаюсь нажать любую из кнопок, ничего не произойдет. Точка довольно проста: нажмите кнопку «+», количество увеличивается, и цена сразу же обновляется, чтобы соответствовать указанной величине, наоборот, для кнопки «-». Я не знаю, что я сделал неправильно, но любое взаимодействие с приложением кнопки вызывает его.Сбой при каждом нажатии кнопок

Вот мой Java:

package com.t99sdevelopment.mobile.eyy; 

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 { 

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

public int quantityValue = 1; 

public void increaseQuantity (View view) { 
    quantityValue = quantityValue + 1; 
    updateQuantityValue(view); 
} 

public void decreaseQuantity (View view) { 
    quantityValue = quantityValue - 1; 
    updateQuantityValue(view); 
} 

public void updateQuantityValue(View view) { 
    updateQuantity(quantityValue); 
    updatePrice(quantityValue); 
} 

private void updateQuantity(int number) { 
    TextView quantity = (TextView) findViewById(
      R.id.quantityValue); 
    quantity.setText(number); 
} 

private void updatePrice(int number) { 
    TextView price = (TextView) findViewById(R.id.priceValue); 
    price.setText(NumberFormat.getCurrencyInstance().format(number * 5)); 
    } 
} 

Вот мой XML:

<TextView 
    android:text="Quantity" 
    android:id="@+id/quantityText" 
    android:textAllCaps="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="16sp" 
    android:textColor="#000000" 
    android:autoText="false" 
    android:paddingBottom="20dp" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center_horizontal"> 

    <TextView 
     android:text="0" 
     android:id="@+id/quantityValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp" /> 

    <Button 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:text="-" 
     android:id="@+id/button" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/quantityValue" 
     android:layout_toStartOf="@+id/quantityValue" 
     android:layout_marginRight="15dp" 
     android:layout_marginEnd="15dp" 
     android:onClick="decreaseQuantity" /> 

    <Button 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:text="+" 
     android:id="@+id/button2" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/quantityValue" 
     android:layout_toEndOf="@+id/quantityValue" 
     android:layout_marginLeft="15dp" 
     android:layout_marginStart="15dp" 
     android:onClick="increaseQuantity" /> 
</RelativeLayout> 

<TextView 
    android:text="Price" 
    android:id="@+id/priceText" 
    android:textAllCaps="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="16sp" 
    android:textColor="#000000" 
    android:autoText="false" 
    android:paddingTop="20dp" /> 

<TextView 
    android:text="$0" 
    android:id="@+id/priceValue" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="20dp" 
    android:paddingBottom="20dp" /> 

<Button 
    android:text="Order" 
    android:textAllCaps="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

Форматирование фактического кода просто отлично, но он получил поддатый, когда я вставил в в стек, поэтому я знаю, что это не проблема.

+0

Показать ошибку из журнала пожалуйста. – Tigger

+0

Добавьте свой метод onClick и updatePrice тоже – behrooz

ответ

0

updateQuantity Изменить Вы и updatePrice метод, как:

private void updateQuantity(int number) { 
    TextView quantity = (TextView) findViewById(
      R.id.quantityValue); 
    quantity.setText(String.valueOf(number)); 
} 

private void updatePrice(int number) { 
    TextView price = (TextView) findViewById(R.id.priceValue); 
    price.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(number * 5))); 
    } 
} 
+0

Спасибо! Это сработало. Я почему-то не понял, когда я устанавливал строковое поле в int. –

Смежные вопросы