2016-11-26 3 views
1

Ich versuche, eine einfache Kaffee-Zähler App zu machen. Es soll auf die Tasten + und - reagieren, indem Sie die Nummer in der Textansicht ändern. Die Schaltfläche "Bestellen" sollte die Kosten für die Herstellung von x Kaffees anzeigen, indem Sie sie mit 5 multiplizieren. Ich erhalte jedoch einen Fehler, wenn ich den Code ausführe.java.lang.IllegalStateException: Methode Eltern oder Vorfahren nicht gefunden Kontext für android: onClick Attribut definiert für Ansicht Klasse

Katze unter:

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 

Aktivität 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); 
    } 
} 

Danke sich die Zeit nehmen, dies zu lesen! Jede Hilfe würde sehr geschätzt werden.

Antwort

0

Ihre PLUS- und MINUS-Methoden müssen öffentlich sein oder die XML kann sie nicht mit der Aktivität verknüpfen.

Verwandte Themen