找不到类' android.graphics.drawable.RippleDrawable',无法执行android的方法:onClick,Android Studio

时间:2017-02-21 21:36:43

标签: java android xml android-layout android-studio

我正在尝试制作一个用于订购咖啡的应用。 (我对此真的很陌生。)

我的应用程序运行正常,直到出现两个错误。现在,它在设备中运行,但是当您尝试触摸按钮时,它会停止。

代码是:

package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {
    int quantity = 0;
    //int priceOfOneCup = 5;

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

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view){
        //calculatePrice(quantity,  priceOfOneCup);
        int price = quantity * 5 ;
        String priceMessage = "Total: " + price + "€" + "\nThank You!";
        displayMessage(priceMessage);
    }

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

    /**
     * This method is called when the minus button is clicked.
     */
    public void decrement(View view) {
        quantity = quantity - 1;
        displayQuantity(quantity);
    }


    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText(number);
    }

    /**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }

    /**
     * This method displays the given text on the screen.
     */
    private void displayMessage(String message) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(message);
    }

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="com.example.android.justjava.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="Quantity"
        android:textAllCaps="true" />

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

        <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:layout_marginLeft="8dp"
            android:layout_marginRight="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>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Price"
        android:textAllCaps="true" />

    <TextView
        android:id="@+id/price_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total: 0€"
        android:textColor="@android:color/black"
        android:textSize="16sp" />


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

终端中的第一个错误:

E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

主要错误:

E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.example.android.justjava, PID: 32585
          java.lang.IllegalStateException: Could not execute method for android:onClick
              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
              at android.view.View.performClick(View.java:4508)
              at android.view.View$PerformClick.run(View.java:18675)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5584)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
              at dalvik.system.NativeStart.main(Native Method)
           Caused by: java.lang.reflect.InvocationTargetException
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
              at android.view.View.performClick(View.java:4508) 
              at android.view.View$PerformClick.run(View.java:18675) 
              at android.os.Handler.handleCallback(Handler.java:733) 
              at android.os.Handler.dispatchMessage(Handler.java:95) 
              at android.os.Looper.loop(Looper.java:136) 
              at android.app.ActivityThread.main(ActivityThread.java:5584) 
              at java.lang.reflect.Method.invokeNative(Native Method) 
              at java.lang.reflect.Method.invoke(Method.java:515) 
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
              at dalvik.system.NativeStart.main(Native Method) 
           Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
              at android.content.res.Resources.getText(Resources.java:1404)
              at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
              at android.widget.TextView.setText(TextView.java:4262)
              at com.example.android.justjava.MainActivity.displayQuantity(MainActivity.java:61)
              at com.example.android.justjava.MainActivity.increment(MainActivity.java:44)
              at java.lang.reflect.Method.invokeNative(Native Method) 
              at java.lang.reflect.Method.invoke(Method.java:515) 
              at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
              at android.view.View.performClick(View.java:4508) 
              at android.view.View$PerformClick.run(View.java:18675) 
              at android.os.Handler.handleCallback(Handler.java:733) 
              at android.os.Handler.dispatchMessage(Handler.java:95) 
              at android.os.Looper.loop(Looper.java:136) 
              at android.app.ActivityThread.main(ActivityThread.java:5584) 
              at java.lang.reflect.Method.invokeNative(Native Method) 
              at java.lang.reflect.Method.invoke(Method.java:515) 
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
              at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

更改此行

quantityTextView.setText(number);

到此:

quantityTextView.setText(String.valueOf(number));

或:

 quantityTextView.setText(number+"");