0

I am trying to set the onclick method for a button (R.id.addclick) inside a fragment. There are no errors, but everytime I start the emulator and press the button, it keeps showing " Unfortunately, ...has stopped". Please give me some advice.

first_layout xml:

Button
        android:id="@+id/addclick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit17"
        android:layout_marginTop="16dp"
        android:layout_toEndOf="@+id/textView32"
        android:layout_toRightOf="@+id/textView32"
        android:onClick="addclick"
        android:text="submit" />

My entire fragment code:

package com.example.administrator.realrandd;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Administrator on 2017-10-28.
 */
public class FirstLayout extends Fragment implements View.OnClickListener {
    View v;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.first_layout, container, false);

        Button b = (Button) v.findViewById(R.id.addclick);
        b.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addclick:

                EditText number1 = (EditText)v.findViewById(R.id.edit1);
                EditText number2 = (EditText)v.findViewById(R.id.edit2);

                TextView result = (TextView)v.findViewById(R.id.Final);

                int n1 = Integer.parseInt(number1.getText().toString());
                int n2 = Integer.parseInt(number2.getText().toString());

                result.setText(Integer.toString(n1+n2));



                break;
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Remove android:onClick="addclick" from your xml and check. And if you are still getting any error, please share the log – Araju Oct 28 '17 at 09:53

2 Answers2

1

Mismatching names: in your layout, you are referring a method called

android:onClick="addclick"

But in your code you set a method called

public void onClick(View v) {

and... "addclick" != "onClick"

You are expected to make the two names match.


So name your method accordigly in your code:

public void addclick(View v) {
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

I think the below text fields are empty. That is why empty string can't cast to int.

EditText number1 = (EditText)v.findViewById(R.id.edit1);
EditText number2 = (EditText)v.findViewById(R.id.edit2);

In this case if you look at the log you will find like

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)

error

Zenith
  • 1,037
  • 1
  • 10
  • 21