-4

I hope you can help me I was trying to look into the other questions but I didnt found what I was trying to do or didn't get it. this is a very simple example of what I'm trying to do. I have this main activity:

public class MainActivity extends Activity {

static EditText num1, num2; //Change EditText from TextView
static TextView num3;       //Textview variable
int x, y, r;

Addition addition=new Addition();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    num1=(EditText)findViewById(R.id.editText);
    num2=(EditText)findViewById(R.id.editText2);
    num3=(TextView)findViewById(R.id.textView);

    x = 0;
    y = 0;

}

public void add (View view){
    setXY();
    r=addition.addResult(x,y);
    num3.setText(String.valueOf(r));

}

public void setXY(){
    x=Integer.parseInt(num1.getText().toString());
    y=Integer.parseInt(num2.getText().toString());
}

}

and I am trying to call that method from this class

public class Addition {

public int addResult(int x, int y){
    return x+y;
}

}

so at the beginning the app crashed because of the null values that was receiving from the layout, but after set the try catch the app was running. The problem comes when I click the "add" button the app crash again. do you guys can tell me why? what am I doing wrong? how should I do it? and if I have an app with more "operations" ,methods, is it worth creating a class with all this operations and call them from the main one? or maybe I shouldn't try to do this?

Greetings!!!

Chiok

Logcat

 07-14 14:35:24.625 5407-5407/com.chiok.x2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                        Process: com.chiok.x2, PID: 5407
                                                        java.lang.IllegalStateException: Could not find method addition(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'button'
                                                            at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4479)
                                                            at android.view.View$DeclaredOnClickListener.onClick(View.java:4443)
                                                            at android.view.View.performClick(View.java:5198)
                                                            at android.view.View$PerformClick.run(View.java:21147)
                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                            at android.os.Looper.loop(Looper.java:148)
                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-14 14:35:25.893 5407-5407/com.chiok.x2 I/Process: Sending signal. PID:       5407 SIG: 9

and this is the xml:

 <RelativeLayout 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: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="com.chiok.x2.MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="48dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText2"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="52dp"
    android:onClick="add" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="71dp"
    android:onClick="addition" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="56dp" />

I guess it have something to do with the editable objets that are null.. but nothing that I try works.

Chiok
  • 1
  • 2
  • 2
    what the error when it crash – Linh Jul 14 '16 at 07:38
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – yennsarah Jul 14 '16 at 07:41
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Linh Jul 14 '16 at 07:53
  • Where is your add button's onclick method? Is it addResult()? – Hilal Jul 14 '16 at 08:10
  • And your logcat must be changed after you fixed R.id.editText parts. Also can we see your layout? – Hilal Jul 14 '16 at 08:11
  • Please also change `setContentView(layout.activity_main)`; to `setContentView(R.layout.activity_main);` – Hilal Jul 14 '16 at 08:12

5 Answers5

1

change these code

num1=(TextView)findViewById(id.editText);
num2=(TextView)findViewById(id.editText2);
num3=(TextView)findViewById(id.editText3);

to

num1=(TextView)findViewById(R.id.editText);
num2=(TextView)findViewById(R.id.editText2);
num3=(TextView)findViewById(R.id.editText3);

and remember to import R.java in your project.

Huang Wei
  • 39
  • 3
1

onClick command which stored in layout need parameter View v in receiver method, so change addResult() to addResult(View v)

0

Use following code.

 public class MainActivity extends Activity {

EditText num1, num2; //Change EditText from TextView
TextView num3;       //Textview variable 
Button add;          //Button variable


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); //Add R.layout.activity_main


    num1=(EditText)findViewById(R.id.editText);  //R.id.editText
    num2=(EditText)findViewById(R.id.editText2); //R.id.editText2
    num3=(TextView)findViewById(R.id.textview); // initialize textview
    add=(Button) findViewById(R.id.add);        //initialize add button

    final Addition x = new Addition();
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         x.addResult();
        }
    });
}
}

public class Addition {

    public void addResult(){
        try {
            num3.setText(String.valueOf((Integer.parseInt(num1.getText().toString())+(Integer.parseInt(num2.getText().toString())))));
        }
        catch (Exception e) {}
    }
}
}
  • thanks! EditText have way much more sence! but the app keeps crushing when pressing the "add" button like in the begining :( – Chiok Jul 14 '16 at 07:56
  • now it says that the num3, num1, num2 "can not solve simbol" – Chiok Jul 14 '16 at 14:11
0

Make your Textview static. Try

 static TextView num1, num2,num3;

Reason

Addition x = new Addition();

In the above code, it is creating a new instance of Addition class which is extending the MainActivity and hence the TextView will be NULL for the new instance. In order to make the TextView initialization available for all instances, make the TextView static.

Sujay
  • 3,417
  • 1
  • 23
  • 25
0

if I have an app with more "operations" ,methods, is it worth creating a class with all this operations and call them from the main one?

Yes it worths that, it will make your code more object oriented. In this case it will make more sense if you change the name of class from Addition to Operations. Also Addition class does not need to be an activity. So don't extend it from MainActivity.

Just send two parameter to that method and make addResult return an int result which is addition of those two parameters.

And in your MainActivity get the return value of your addResult method and setText the edittext3.

public class MainActivity extends Activity {

TextView num1, num2,num3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    num1=(TextView)findViewById(R.id.editText);
    num2=(TextView)findViewById(R.id.editText2);
    num3=(TextView)findViewById(R.id.editText3);

    // here I am trying to call the method
    Addition x = new Addition();
    int param1 = Integer.parseInt(num1.getText().toString());
    int param2 = Integer.parseInt(num2.getText().toString());
    int result = x.addResult(param1, param2);
    num3.setText(String.valueOf(result));
  }
}

And your Addition Class can be like this;

public class Addition {

    public int addResult(int param1, int param2){

        return param1 + param2;

    }
}

Summary I changed setContentView(R.layout.activity_main); Delete extends MainActivity in your Addition class. Also if the error still persists, I need to see your xml layout where you define your textviews and button.

Hilal
  • 902
  • 2
  • 22
  • 47