2017-03-19 74 views
0

我試圖做一個使用按鈕的自定義視圖,但是,兩個按鈕的android:Onclick導致應用程序崩潰。 ClickListeners也不起作用。Android:Onclick找不到onclick方法

我從運行調試工具得到的錯誤是:

java.lang.IllegalStateException: Could not find a method addMenu(View) in the activity class com.android.tools.fd.runtime.BootstrapApplication for onClick handler on view class android.widget.Button with id 'increase' 

這是我的Java代碼:

public class MenuItems extends LinearLayout{ 

    private View plusButton; 
    private View negateButton; 
    private EditText priceBox; 
    private TextView total; 
    private TextView name; 
    private int totalItems; 
    private double totalPrice; 
    private double price; 

    public MenuItems(Context context) { 
     super(context); 
     init(); 
    } 

    public MenuItems(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 
    public void init() 
    { 

     LayoutInflater inflater=LayoutInflater.from(getContext()); 
     inflater.inflate(R.layout.menu_item,this); 

     plusButton=findViewById(R.id.increase); 
     negateButton=findViewById(R.id.decrease); 
     total=(TextView)findViewById(R.id.total); 
     name=(EditText)findViewById(R.id.ItemName); 
     priceBox=(EditText)findViewById(R.id.Price) ; 

     total.setText("5"); 
     negateButton.setBackgroundColor(Color.RED); 

     plusButton.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 
       plusButton.setBackgroundColor(Color.RED); 
       increase(); 
      } 
     }); 
     negateButton.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 
       decrease(); 
       negateButton.setBackgroundColor(Color.RED); 
      } 
     }); 

     price=0; 
     totalItems=0; 
     upDateTotal(); 
     String pString=priceBox.getText().toString(); 
     price=Double.parseDouble(pString); 
    } 
    public void increase(View view) 
    { 
     totalItems++; 
     upDateTotal(); 
     System.out.println("plus"); 
     plusButton.setBackgroundColor(Color.RED); 

    } 

    public void decrease(View v) 
    { 
     if(totalItems>0) 
      totalItems--; 
     upDateTotal(); 
     System.out.println("minus"); 
    } 

這是我所有的xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/menu_items" 
    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="com.mohs.joey.seniorproject.MenuItems"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

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

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="textPersonName" 
       android:ems="10" 
       android:id="@+id/ItemName" 
       android:layout_weight="1" 
       android:textColor="@color/colorAccent" 
       android:text="name" /> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:ems="10" 
       android:id="@+id/Price" 
       android:textColor="@color/colorAccent" 
       android:hint="Price" 
       android:shadowColor="@color/colorAccent" 
       android:text="0.00" /> 

     </LinearLayout> 

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

      <Button 
       android:text="-" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/decrease" 
       android:layout_weight="1" 
       android:backgroundTint="@color/colorAccent" 
       android:onClick="increase"/> 

      <TextView 
       android:text="Amount" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/total" 
       android:layout_weight="1" 
       android:textColor="@color/colorAccent" /> 

      <Button 
       android:text="+" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/increase" 
       android:layout_weight="1" 
       android:backgroundTint="@color/colorAccent" 
       android:elevation="0dp" 
       android:onClick="increase" /> 
     </LinearLayout> 
    </LinearLayout> 

</RelativeLayout> 

我在一個單獨的類中訪問此方法中的MenuItems:

public void addMenu(View view) 
    { 
     LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = vi.inflate(R.layout.menu_item, null); 
     v.setId(menu); 
     ViewGroup insertPoint = (ViewGroup) findViewById(R.id.backLayer); 
     insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
     menu++; 
    } 

這是我的新的主要活動:

package com.mohs.joey.seniorproject; 

import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.PopupWindow; 
import android.widget.TextView; 

public class ConcessionMenus extends AppCompatActivity { 
    int menu=0; 

    private View plusButton; 
    private View negateButton; 
    private EditText priceBox; 
    private TextView total; 
    private TextView name; 
    private int totalItems; 
    private double totalPrice; 
    private double price; 

    PopupWindow popup; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_concession_menus); 
     plusButton=findViewById(R.id.increase); 
     negateButton=findViewById(R.id.decrease); 
     total=(TextView)findViewById(R.id.total); 
     name=(EditText)findViewById(R.id.ItemName); 
     priceBox=(EditText)findViewById(R.id.Price) ; 
//  TextView totalPrice=(TextView)findViewById(R.id.totalPrice); 
    } 

    public void addMenu(View view) 
    { 
     LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View vw = vi.inflate(R.layout.menu_item, null); 
     vw.setId(menu); 
     ViewGroup insertPoint = (ViewGroup) findViewById(R.id.backLayer); 
     insertPoint.addView(vw, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
     menu++; 
    } 
    public Intent newIntent() 
    { 
     Intent intent=new Intent(this,Menu.class); 
     return intent; 
    } 
    public void increase(View v) 
    { 
     totalItems++; 
     upDateTotal(); 
     System.out.println("plus"); 
     plusButton.setBackgroundColor(Color.RED); 

    } 

    public void decrease(View v) 
    { 
     if(totalItems>0) 
      totalItems--; 
     upDateTotal(); 
     System.out.println("minus"); 
    } 

    public void upDateTotal() 
    { 
     total.setText(""+totalItems); 
     totalPrice=price*totalItems; 
    } 

    public double totalPrice() 
    { 
     return totalPrice; 
    } 

} 

任何幫助,將不勝感激。

+0

你在哪裏訪問類的菜單項,你不能用這些ID的工作原理? – ArolaAb

+0

我添加了方法從一個單獨的類,我從中訪問MenuItems –

+0

請給我們展示一些代碼 – ArolaAb

回答

0

問題是,當該按鈕要當你點擊它要執行的操作,它應該能夠找到方法increase()MainActivity類,因爲該方法increase()是一個其他類,按鈕將永遠找不到該方法,他們會崩潰,因爲你的佈局XML設置爲您的主要活動而不是爲類MenuItem,修復,只是將這些的方法您MainActivity class

OR

您通過按鈕來參考類MenuItems並做你想要的

PS:在IDS可以通過鍵入R.id發現,但因爲你設置的內容,以便您MainActivity

+0

我將所有方法和其他參考移至主要活動,但我仍然得到完全相同的錯誤 –

+0

請向我們展示主要活動的所有代碼 – ArolaAb

+0

我添加了新的主要活動 –

相關問題