似乎无法理解为什么我的应用程序强行关闭

时间:2016-11-05 14:52:45

标签: android

因此,当我按下设备上的递增或递减按钮时,应用程序强制关闭。我尝试使用调试器,但似乎无法找出问题所在。它基本上是一个显示订单摘要的单个屏幕应用程序。

以下是XML代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/name_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="QUANTITY!" />


        <CheckBox
            android:id="@+id/whipped_cream_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="24dp"
            android:text="Whipped Cream"
            android:textSize="16sp" />



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:onClick="decrement"
                android:text="-" />

            <TextView
                android:id="@+id/quantity_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:text="0"
                android:textColor="@android:color/black"
                android:textSize="16sp" />

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:onClick="increment"
                android:text="+" />

        </LinearLayout>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:onClick="submitOrder"
            android:text="Order" />

        <TextView
            android:id="@+id/order_summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"/>

    </LinearLayout>
</ScrollView>

JAVA代码:

    package com.example.android.timeforacoffee;

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

public class MainActivity extends AppCompatActivity {

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

    int quantity=0;
    String priceMessage="Name: Moksh \n" + "Your total is ₹" +quantity;

    public void displayQuanity(int quantity) {
        TextView quantity_value=(TextView) findViewById(R.id.quantity_text_view);
        quantity_value.setText(quantity);
    }

    /**
     *This method is called when the - button is pressed.
     */
    public void decrement(View view) {
        displayQuanity(quantity - 1);
    }

    /**
     *
     * This method is called when the + button is clicked.
     */
    public void increment(View view) {
        displayQuanity(quantity + 1);
    }


    public void displayMessage(String priceMessage) {
        TextView order_summary=(TextView) findViewById(R.id.order_summary);
                order_summary.setText(priceMessage);
    }

    /**
     * This method is called when the Order button is clicked.
     */

    public void submitOrder(View view) {
        displayMessage(priceMessage);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题出在下面的代码行

   quantity_value.setText(quantity);

请注意,当您将任何整数传递给setText函数时,android会将整数视为对字符串reource的引用,并在资源文件中搜索该字符串,如果找不到该字符串,则您的应用程序将崩溃{ {1}}。 您可以通过将整数显式解析为字符串来解决此问题,如下所示

android.content.res.Resources$NotFoundException