0

I just have a button in my activity_main.xml view as below:

<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/capt_sig" android:onClick="openCaptureActivity" //click event here android:text="@string/capture_signature" android:layout_below="@+id/result" android:layout_alignParentEnd="true" android:layout_marginTop="235dp" android:clickable="true" android:typeface="sans" /> 

and have below onclick event written in MainActivity.java

public void openCaptureActivity(View view){ setContentView(R.layout.activity_capture_signature); } 

Now onClick of the button it opens below activity with name activity_capture_signature.xml and here are the contents.

<android.gesture.GestureOverlayView android:id="@+id/signaturePad" android:layout_width="match_parent" android:layout_height="100dp" android:layout_weight="5" android:background="#FFF" android:eventsInterceptionEnabled="true" android:fadeEnabled="false" android:gestureColor="#000" android:gestureStrokeLengthThreshold="0.1" android:gestureStrokeType="multiple" android:orientation="horizontal"> </android.gesture.GestureOverlayView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/saveSig" android:layout_marginTop="100dp" android:onClick="SaveSignature" //click event here android:text="Capture"/> 

I have added a onClick for button as well here as you can see and the event is written in CaptureSignatureActivity.java as below:

public void SaveSignature(View v){ //Do some stuff } 

Even with this setups, the SaveSignature event throws FATAL EXCEPTION as below:

java.lang.IllegalStateException: Could not find method SaveSignature(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'saveSig'

I am still not able understand that even after adding click event in activity, why this exception is occurring. Is this because of the way I open the activity from parent? Is there anything am missing here?

2
  • Try to: Build > Clean project Commented Aug 3, 2016 at 7:54
  • Have you initialized Buttons at Activtiy.java?? Commented Aug 3, 2016 at 7:55

4 Answers 4

3

You need to start the new activity CaptureSignatureActivity.java in openCaptureActivity() method. Currently you are trying to inflate the layout inside MainActivity itself. Do

public void openCaptureActivity(View view){ startActivity(new Intent(MainActivity.this, CaptureSignatureActivity.class)); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Oh yea.. My understanding on this was much low.. Thanks for the timely help.. :)
3
Use different method for clicking button.for example btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //do stuff } }); 

1 Comment

Tried this.. But the issue was different here as mentioned in accepted answer.. :)
1
public void openCaptureActivity(View view){ setContentView(R.layout.activity_capture_signature); } 

This method does not change your activity. You are still in the MainActivity. If you define click methods in xml you must define that methods in MainActivity or use OnClickListener.

Comments

1
public void openCaptureActivity(View view){ setContentView(R.layout.activity_capture_signature); } 

see carefully setContentView(); this method is use to attach/set layout content to your Actiity. May be you used that method inside your onCreate() method also.

in your activity_main.xml

<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/capt_sig" android:onClick="openCaptureActivity" //click event here android:text="@string/capture_signature" android:layout_below="@+id/result" android:layout_alignParentEnd="true" android:layout_marginTop="235dp" android:clickable="true" android:typeface="sans" /> 

And then in MainActivity.java

use :

public void openCaptureActivity(View view){ //setContentView(R.layout.activity_capture_signature); startActivity(new Intent(MainActivity.this,CaptureSignatureActivity.class)); } 

now your CaptureSignatureActivity it should be like this

in its onCreate() method use this line to set content that you had used wrong in previous activity by setContentView(R.layout.activity_capture_signature);

and then you can use second click method for button in CaptureSignatureActivity

public void SaveSignature(View v){ //Do some stuff } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.