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;
}
}
}