0

Trying to write my first app on android. But the app crashes(FC) on the virtual device when clicking any of buttons. Please take a look.. the MainActivity.java:

public class MainActivity extends Activity {
public EditText t1 = null, t2 = null;
Button b1 = null, b2 = null;
TextView tv1 = null,tv2 = null;
int a = 0, b = 0, S1 = 0, S2 = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1=(EditText)findViewById(R.id.editText1);
    t2=(EditText)findViewById(R.id.EditText2);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    tv1=(TextView)findViewById(R.id.TextView1);
    tv2=(TextView)findViewById(R.id.TextView2);


}
public void onClick1() 
{
    tv1.clearComposingText();
  a=Integer.parseInt(t1.getText().toString());
   tv1.setText(S1+=a);
   }

public void onClick2() 
{
    tv2.clearComposingText();
  b=Integer.parseInt(t2.getText().toString());
   tv2.setText(S2+=b);
   }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

activity_main.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" >

<Button
    android:id="@+id/button1"
    android:layout_width="80dp"
    android:layout_height="60dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="30dp"
    android:text="Add"
    android:onClick="onClick1" />

<Button
    android:id="@+id/button2"
    android:layout_width="80dp"
    android:layout_height="60dp"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_marginLeft="26dp"
    android:layout_toRightOf="@+id/button1"
    android:text="Add"
    android:onClick="onClick2" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="80dp"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="24dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/EditText2"
    android:layout_width="80dp"
    android:layout_height="60dp"
    android:layout_alignBaseline="@+id/editText1"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignLeft="@+id/button2"
    android:ems="10" />

<TextView
    android:id="@+id/TextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/editText1"
    android:layout_centerVertical="true"
    android:text="" />

<TextView
    android:id="@+id/TextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextView1"
    android:layout_alignLeft="@+id/EditText2"
    android:text="" />

and logcat log file:

10-14 17:36:35.714: E/AndroidRuntime(697): FATAL EXCEPTION: main
10-14 17:36:35.714: E/AndroidRuntime(697): java.lang.IllegalStateException: Could not find a method onClick1(View) in the activity class com.example.blot.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.view.View$1.onClick(View.java:2131)
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.view.View.performClick(View.java:2485)
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.view.View$PerformClick.run(View.java:9080)
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.os.Handler.handleCallback(Handler.java:587)
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.os.Looper.loop(Looper.java:123)
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-14 17:36:35.714: E/AndroidRuntime(697):  at java.lang.reflect.Method.invokeNative(Native Method)
10-14 17:36:35.714: E/AndroidRuntime(697):  at java.lang.reflect.Method.invoke(Method.java:507)
10-14 17:36:35.714: E/AndroidRuntime(697):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-14 17:36:35.714: E/AndroidRuntime(697):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-14 17:36:35.714: E/AndroidRuntime(697):  at dalvik.system.NativeStart.main(Native Method)
10-14 17:36:35.714: E/AndroidRuntime(697): Caused by: java.lang.NoSuchMethodException: onClick1
10-14 17:36:35.714: E/AndroidRuntime(697):  at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
10-14 17:36:35.714: E/AndroidRuntime(697):  at java.lang.Class.getMethod(Class.java:962)
10-14 17:36:35.714: E/AndroidRuntime(697):  at android.view.View$1.onClick(View.java:2124)
10-14 17:36:35.714: E/AndroidRuntime(697):  ... 11 more
Cœur
  • 37,241
  • 25
  • 195
  • 267
Arsen
  • 3
  • 1

7 Answers7

0

It would seem that your app doesn't know what to do when someone clicks on your button.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • doesn't this <> mean that when clicking button2 the function "onClick2" should be called? – Arsen Oct 14 '12 at 12:44
0

Like the error tells you it needs a parameter 'View' in the method.

Change your onclick methods to accept the View parameter. You have to do the same for all your other button onclick methods.

public void onClick1(View v) {
    // Code
|
Jap Mul
  • 17,398
  • 5
  • 55
  • 66
0

you forgot to pass View in onClick1():

public void onClick1(View v){
    //your code here
}

public void onClick2(View v){
    //your code here
} 

For further reading: Best practice for defining button events in android

Community
  • 1
  • 1
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
0

The OnClick method must accepts View parameter

public void OnClick1(View view) {
     ...
 }

The same for OnClick2

Btw it's advise but you should read carefully the error messages:

java.lang.IllegalStateException: Could not find a method onClick1(View)

Notice that it hints that it needs View parameter.

Other things I noticed:

you should use @+id syntax to declare a new id for a resource. Once you've done this you should use @id to refer to the resouce. In other words you need to have "@+id/button1" only once (in the Button1 declaration), after that use "@id/button1" to refer to it. The same counts for all other ids of course (EditText and so on...)

tozka
  • 3,211
  • 19
  • 23
  • after adding View params I'm still getting Force Close after clicking buttons. – Arsen Oct 14 '12 at 12:49
  • What error do you get? Does logcat says anything? – tozka Oct 14 '12 at 12:52
  • http://dl.dropbox.com/u/78302442/log.txt here's logcat's new log,after adding view params – Arsen Oct 14 '12 at 12:56
  • I think you have problems with the ids, see my editted answer above. – tozka Oct 14 '12 at 13:05
  • but where else do I use @id besides activity_main.xml? – Arsen Oct 14 '12 at 13:16
  • `layout_alignBaseline="@+id/button1"` should be `layout_alignBaseline="@id/button1"` The plus sign indicates that you are defining id (which you've already done). The same in all other places. You define id (`@+id`) for resource only once, then you refer to it (`@id`). – tozka Oct 14 '12 at 13:26
  • can't find anywhere @+id besides the activity_main.xml.. – Arsen Oct 14 '12 at 14:03
0

xml files don't like it when you have capital letters it the method name. Change the method names to "onclick1" instead of "onClick1" and it should work. At least it worked for me after copy pasting your project into my own IDE...

also add the View parameter as mentioned in the other answers here.

Nospherus
  • 186
  • 2
  • 4
0

you should add the listener programmatically

b1.setonClickListener(this);

then in your activity. implement the interface onclickListener.

in your override method onclick. use a switch statement to determine what view is accessing it.

or you can create a new onclicklistener.

b1.setonClickListener(new onclicklistner....)

WillingLearner
  • 739
  • 6
  • 10
0

Simply try this.....

- First remove the onClick1() and onClick2() methods.

- Use Anonymous Inner class for registering and implementing the method onClick() of Interface OnClickListener

b1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                          tv1.clearComposingText();
                          a=Integer.parseInt(t1.getText().toString());
                          tv1.setText(S1+=a);


            }
        });


b2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                     tv2.clearComposingText();
                         b=Integer.parseInt(t2.getText().toString());
                         tv2.setText(S2+=b);

            }
        });
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75