简体   繁体   English

添加参数do方法后,应用崩溃

[英]App crashes after adding parameter do method

I've got problem with my simple calculator app: 我的简单计算器应用出现问题:

When I've got method like this 当我有了这样的方法

public void WypiszWartoscPrzycisku(View sender)

it work fine and everything is okay, but when I add float, I mean: 它工作正常,一切都很好,但是当我添加float时,我的意思是:

public void WypiszWartoscPrzycisku(View sender, float num1)

it crashes. 它崩溃了。 I'm not sure what should I give more, becouse I don't do anything with num1 and app still crashes. 我不确定应该给什么,因为我对num1不做任何事情,而且应用仍然崩溃。

XML(sorry, I'm not native english so a lot of things I named in my language) XML(对不起,我不是英语母语,所以我用自己的语言命名了很多东西)

<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:orientation="vertical"
    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=".MainActivity" >

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

        <Button
            android:id="@+id/button_AC"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/AC"
            android:onClick="WypiszWartoscPrzycisku"/>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="@string/wynik"
            android:inputType="number" >

            <requestFocus />
        </EditText>
    </LinearLayout>

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

        <Button
            android:id="@+id/button_Siedem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/siedem" 
            android:onClick="WypiszWartoscPrzycisku"/>

        <Button
            android:id="@+id/button_Osiem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/osiem"
            android:onClick="WypiszWartoscPrzycisku" />

        <Button
            android:id="@+id/button_Dziewiec"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/dziewiec"
            android:onClick="WypiszWartoscPrzycisku" />


        <Button
            android:id="@+id/button_Plus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/plus" 
            android:onClick="WypiszWartoscPrzycisku"/>
    </LinearLayout>

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

        <Button
            android:id="@+id/button_Cztery"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/cztery" 
            android:onClick="WypiszWartoscPrzycisku"/>

        <Button
            android:id="@+id/button_Piec"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/piec" 
            android:onClick="WypiszWartoscPrzycisku"/>

        <Button
            android:id="@+id/button_Szesc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/szesc"
            android:onClick="WypiszWartoscPrzycisku" />

        <Button
            android:id="@+id/button_Minus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/minus"
            android:onClick="WypiszWartoscPrzycisku" />
    </LinearLayout>

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

        <Button
            android:id="@+id/button_Jeden"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/jeden" 
            android:onClick="WypiszWartoscPrzycisku"/>

        <Button
            android:id="@+id/button_Dwa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/dwa" 
            android:onClick="WypiszWartoscPrzycisku"/>

        <Button
            android:id="@+id/button_Trzy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/trzy"
            android:onClick="WypiszWartoscPrzycisku" />

        <Button
            android:id="@+id/button_Mnozenie"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/mnozenie"
            android:onClick="WypiszWartoscPrzycisku" />
    </LinearLayout>

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

        <Button
            android:id="@+id/button_Zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/zero" 
            android:onClick="WypiszWartoscPrzycisku"/>

        <Button
            android:id="@+id/button_Kropka"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/kropka" />

        <Button
            android:id="@+id/buttonRownasie"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/rownasie" 
            android:onClick="WypiszWartoscPrzycisku"/>

        <Button
            android:id="@+id/button_dzielenie"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/dzielenie"
            android:onClick="WypiszWartoscPrzycisku" />
    </LinearLayout>

</LinearLayout>

EDIT: 编辑:

Thank you @beworker and @Raghunandan for answers. 谢谢@beworker和@Raghunandan的回答。 I get it. 我知道了。 I though adding parameter do method will solve my main problem. 我虽然添加了参数do方法将解决我的主要问题。 My code is (changed to english names) switch (bt.getId()){ 我的代码是(已更改为英文名称)开关(bt.getId()){

    case R.id.button_AC:
        display.setText("");
        break;

    case R.id.button_Plus:
        float num1 = Float.parseFloat(actualText.toString());
        display.setText("");
        break;

    case R.id.buttonEquals:
        float num2 = Float.parseFloat(actualText.toString());
        float num3 = num1+num2;
        String result  = Float.toString(num3);
        display.setText(wynik);

And it says, that in last case R.id.Equals , num1 is not declared. 它说,在最后case R.id.Equalsnum1没有声明。 Thanks:) 谢谢:)

Looks like in your layout xml you have a button with the below attributes 看起来在布局xml中,您有一个具有以下属性的按钮

  android:onClick="WypiszWartoscPrzycisku"

So you need to have 所以你需要

  public void WypiszWartoscPrzycisku(View sender) // method signature should be like this
  {

  }  

android:onClick

Name of the method in this View's context to invoke when the view is clicked. 单击该视图时在此View上下文中调用的方法的名称。 This name must correspond to a public method that takes exactly one parameter of type View. 该名称必须对应于一个公共方法,该方法只使用View类型的一个参数。 For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity) . 例如, 如果您指定android:onClick =“ sayHello”,则必须声明上下文(通常是Activity)的公共void sayHello(View v)方法

http://developer.android.com/reference/android/view/View.html#attr_android:onClick http://developer.android.com/reference/android/view/View.html#attr_android:onClick

If you have this 如果你有这个

 public void WypiszWartoscPrzycisku(View sender, float num1)

your activity does not have WypiszWartoscPrzycisku(View sender) which is must since you have android:onClick="WypiszWartoscPrzycisku" and when you click the button you get a exception could not find method WypiszWartoscPrzycisku(View) in Activity. 您的活动没有WypiszWartoscPrzycisku(View sender) ,因为您拥有android:onClick="WypiszWartoscPrzycisku" ,所以必须这样android:onClick="WypiszWartoscPrzycisku"并且当您单击按钮时,您将获得一个异常,无法在Activity中找到方法WypiszWartoscPrzycisku(View)

Edit: 编辑:

    case R.id.button_Plus:
    float num1 = Float.parseFloat(actualText.toString()); // declared and initialized here
    display.setText("");
    break;

But you are using it here 但是你在这里用

    case R.id.buttonEquals:
    float num2 = Float.parseFloat(actualText.toString());
    float num3 = num1+num2;

You need to declare num1 as a class member 您需要将num1声明为类成员

Edit 2: 编辑2:

   case R.id.buttonEquals: 
   //  check if you have a button with id R.id.buttonEquals

You should always use first method only. 您应该始终仅使用第一种方法。 This is the method Android will look for. 这是Android会寻找的方法。 You can use it with every Button, but then you need to check for sender id to execute appropriate action. 您可以将其与每个Button一起使用,但是随后需要检查发送者ID以执行适当的操作。 Here is an example. 这是一个例子。

public void WypiszWartoscPrzycisku(View sender) {
  if (sender.getId() == R.id.button_Cztery) {
    // execute code for button_Cztery
  } else if (sender.getId() == R.id.button_Dziewiec) {
    // execute code for button_Dziewiec
  } else if (sender.getId() == R.id.button_Plus) {
    // execute code for button_Plus
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM