2016-11-26 23 views
1

我想做一個簡單的咖啡計數器應用程序。它應該通過改變文本視圖中的數字來響應按鈕+和 - 。訂單按鈕應顯示生產x咖啡的成本乘以5.但是,當我運行代碼時出現錯誤。java.lang.IllegalStateException:無法找到方法父或祖先上下文的android:onClick屬性定義在視圖類

貓如下:

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); 
    } 
} 

謝謝花時間閱讀本文!任何幫助將不勝感激。

回答

0

您的PLUS和MINUS方法必須是公開的,或者XML無法將它們鏈接到活動。

相關問題