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" />
的實際代碼的格式是蠻好的,但是當我粘貼,它出現了扭曲進入堆棧,所以我知道這不是問題。
從日誌顯示錯誤,請。 – Tigger
也添加onClick方法和updatePrice方法 – behrooz