简体   繁体   中英

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:

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.

XML(sorry, I'm not native english so a lot of things I named in my language)

<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. I get it. I though adding parameter do method will solve my main problem. My code is (changed to english names) switch (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. Thanks:)

Looks like in your layout xml you have a button with the below attributes

  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. This name must correspond to a public method that takes exactly one parameter of type 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) .

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.

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

Edit 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. You can use it with every Button, but then you need to check for sender id to execute appropriate action. 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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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