java.lang.IllegalStateException:无法找到方法父或祖先上下文为android:onClick属性在视图类上定义

时间:2016-11-26 20:52:39

标签: java android xml

我正在尝试制作一个简单的咖啡柜台App。它应该通过更改文本视图中的数字来响应按钮+和 - 。订单按钮应显示生产x咖啡的成本,将其乘以5.但是,当我运行代码时出现错误。

以下猫:

11-26 12:37:17.106 14946-14946/jrjava.justjava E/AndroidRuntime: FATAL EXCEPTION: main
Process: jrjava.justjava, PID: 14946
java.lang.IllegalStateException: Could not find method MINUS(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'decrement'
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
     at android.view.View.performClick(View.java:4785)
     at android.view.View$PerformClick.run(View.java:19884)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5343)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
11-26 12:42:17.138 14946-14946/jrjava.justjava I/Process: Sending signal. PID: 14946 SIG: 9

活动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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="jrjava.justjava.MainActivity"
    android:orientation="vertical">

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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/plus"
    android:onClick="PLUS"
    android:text="+"
    />

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textColor="#000000"
    android:paddingBottom="16dp"
    android:textSize="16sp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/decrement"
    android:onClick="MINUS"
    android:text="-"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="price"
    android:textAllCaps="true"
    android:textSize="16sp"
    android:paddingBottom="16dp"
    android:textColor="#000000"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$0"
    android:paddingBottom="16dp"
    android:textColor="#000000"
    android:id="@+id/price_text_view"
    />

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Order"
    android:textColor="#000000"
    android:onClick="submitOrder"/>
</LinearLayout>

MainActivity.java

package jrjava.justjava; /**
 * Add your package below. Package name can be found in the project's AndroidManifest.xml file.
 * This is the package name our example uses:
 *
 * 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;

    @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) {
        displayPrice(quantity*5);
    }
    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(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));
    }

    private void MINUS(View view) {
        display(2);
    }

    private void PLUS(View view) {
        display(4);
    }
}

感谢您抽出宝贵时间阅读本文!非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的PLUS和MINUS方法必须是公共的,否则XML无法将它们链接到活动。