有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:Onclick未找到Onclick方法

我试图创建一个使用按钮的自定义视图,但是,这两个按钮的安卓:Onclick会导致应用程序崩溃。ClickListeners也不工作

运行调试工具时出现的错误是:

java.lang.IllegalStateException: Could not find a method addMenu(View) in the activity class com.安卓.tools.fd.runtime.BootstrapApplication for onClick handler on view class 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/menu_items"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mohs.joey.seniorproject.MenuItems">

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

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

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

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

        </LinearLayout>

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

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

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

            <Button
                安卓:text="+"
                安卓:layout_width="50dp"
                安卓:layout_height="wrap_content"
                安卓:id="@+id/increase"
                安卓:layout_weight="1"
                安卓:backgroundTint="@color/colorAccent"
                安卓:elevation="0dp"
                安卓: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 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.graphics.Color;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.EditText;
import 安卓.widget.PopupWindow;
import 安卓.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;
    }

}

任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    问题是,当你点击按钮想要执行操作时,它应该能够在你的MainActivity类中找到方法increase(),因为方法increase()在另一个类上,按钮将永远找不到方法,它们将崩溃,因为你将你的布局xml设置为你的主活动,而不是类MenuItem,要解决这个问题,只需将这些方法移到MainActivity class

    或者

    将按钮的引用传递给类MenuItems,然后在那里做你想做的事情

    PS:可以通过键入R.id来找到id,但您无法使用这些id,因为您将内容视图设置为MainActivity