每当我点击按钮,我的应用程序崩溃

时间:2017-05-23 16:57:51

标签: android

每当我在设备上运行应用程序时单击增量按钮,我的应用程序意外强制执行 这是我的activity_main.xml代码

    <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tools:context=".MainActivity"
    >
    <RelativeLayout
        android:padding="20dp"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="center_vertical"
        >
        <EditText
            android:id="@+id/name_text_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Name"
            android:inputType="textCapWords"
            ></EditText>
        <TextView
            android:layout_centerVertical="true"
            android:id="@+id/heading1"
            style="@style/HeaderTextStyle"
            android:text="Toppings"
            android:layout_below="@id/name_text_field"
            />
        <CheckBox
            android:layout_below="@id/heading1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/whipped_cream"
            android:textSize="16sp"
            android:paddingLeft="24dp"
            android:layout_marginTop="20dp"
            android:text="Whipped Cream"
            />
        <CheckBox
            android:layout_marginTop="20dp"
            android:layout_below="@id/whipped_cream"
            android:id="@+id/chocolate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="chocolate"
            android:textSize="16sp"
            android:paddingLeft="24dp"
            />
        <TextView
            android:layout_below="@id/chocolate"
            android:id="@+id/quantity_text_view"
            style="@style/HeaderTextStyle"
            android:text="Quantity"
            android:layout_marginBottom="20dp"
            />
        <Button
            android:layout_below="@id/quantity_text_view"
            android:id="@+id/decrement_button"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:onClick="decrement"
            android:text="-"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:paddingLeft="10dp"
            android:layout_below="@id/quantity_text_view"
            android:paddingRight="10dp"
            android:textSize="15sp"
            android:layout_toRightOf="@id/decrement_button"
            android:id="@+id/initial_qty"
            android:gravity="center_vertical"
            android:text="1"
            />
        <Button
            android:layout_toRightOf="@id/initial_qty"
            android:layout_below="@id/quantity_text_view"
            android:id="@+id/increment_button"
            android:onClick="increment"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:text="+"
            />
        <Button
            android:id="@+id/submit_button"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="order"
            android:onClick="submitOrder"
            android:gravity="center_vertical"
            android:layout_below="@id/decrement_button"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center_vertical"
            android:id="@+id/order_summary_text_view"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:textSize="16sp"
            android:layout_below="@id/submit_button"
            />
    </RelativeLayout>

</ScrollView>

这是我的MainActivity.java文件

package com.orton.birthdaycard;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
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);
    }
    int quantity=1;
    private void increment(View view)
    {
        if(quantity==100)
            return;
        else
        quantity++;
        displayQuantity(quantity);
    }
    private void decrement(View view)
    {
        if(quantity==1)
            return;
        else
            quantity--;
        displayQuantity(quantity);
    }
    private void displayQuantity(int qty)
    {
        TextView quantityTextView=(TextView) findViewById(R.id.initial_qty);
        quantityTextView.setText(String.valueOf(quantity));
        Log.v("MainActivity","the quantity is " + quantity);
    }
    int basePrice;
    private int calculatePrice(boolean hasWhippedCream,boolean hasChocolate)
    {
        basePrice = 5;
        if(hasWhippedCream==true)
            basePrice += 1;
        if(hasChocolate==true)
            basePrice +=2 ;
        return basePrice=basePrice*quantity;

    }
    String priceMessage="";
    private void submitOrder(View view)
    {
        EditText nameField = (EditText) findViewById(R.id.name_text_field);
        String name = nameField.getText().toString();

        CheckBox whippedCream  = (CheckBox) findViewById(R.id.whipped_cream);
        boolean hasWhippedCream = whippedCream.isChecked();
        CheckBox chocolate = (CheckBox) findViewById(R.id.chocolate);
        boolean hasChocolate = chocolate.isChecked();

        int price=calculatePrice(hasWhippedCream,hasChocolate);
        String orderSummary=createOrderSummary(name,price,hasWhippedCream,hasChocolate);
        displayMessage(orderSummary);
    }
    private void displayMessage(String orderSummary)
    {
        TextView summary= (TextView) findViewById(R.id.order_summary_text_view);
        summary.setText(""+orderSummary);
    }
    private String createOrderSummary(String name, int price, boolean hasWhippedCream, boolean hasChocolate)
    {
        String priceMessage = "Name :" + name;
        if(hasWhippedCream)
        priceMessage += "\nWhipped Cream Added" ;
        if(hasChocolate)
            priceMessage += "\nChocolate Added" ;
        priceMessage+="\nQuantity : "+quantity;
        priceMessage+="\nTotal : $"+price;
        priceMessage+="Thank You!" ;
        return priceMessage;
    }

}

这是调试报告 05-23 22:14:45.695 29736-29736 / com.orton.birthdaycard E / AndroidRuntime:FATAL EXCEPTION:main                                                                         过程:com.orton.birthdaycard,PID:29736                                                                         java.lang.IllegalStateException:无法在父级或祖先语句中找到方法增量(View)for android:在视图类android.support.v7.widget.AppCompatButton上定义的onClick属性,其id为“increment_button”                                                                             在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)                                                                             在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)                                                                             在android.view.View.performClick(View.java:5612)                                                                             在android.view.View $ PerformClick.run(View.java:22285)                                                                             在android.os.Handler.handleCallback(Handler.java:751)                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                             在android.os.Looper.loop(Looper.java:154)                                                                             在android.app.ActivityThread.main(ActivityThread.java:6123)                                                                             at java.lang.reflect.Method.invoke(Native Method)                                                                             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

2 个答案:

答案 0 :(得分:2)

您的方法必须使用public修饰符:

public void increment(View view)
    {
        if(quantity==100)
            return;
        else
        quantity++;
        displayQuantity(quantity);
    }

答案 1 :(得分:0)

这样做

increment_button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

而不是android:onClick="increment"