-4

Code:

public class HomeActivity extends AppCompatActivity {
    @Override protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_home); 
    } 
    public void onClick() {
        Intent i=new Intent(this,Login.class); 
        startActivity(i); 
    }
} 

Crash:

--------- beginning of crash

06-30 18:57:36.762 3115-3115/com.example.aakash.rozgari E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.aakash.rozgari, PID: 3115
    java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button2'
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25883)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6642)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
06-30 18:57:36.796 3115-3115/com.example.aakash.rozgari I/Process: Sending signal. PID: 3115 SIG: 9
fabian
  • 80,457
  • 12
  • 86
  • 114
Akash
  • 29
  • 6
  • you need to post the associated code. the stacktrace is not enough to go on. – Timothy Winters Jun 30 '18 at 13:20
  • code is building successfully and running fine.But it fails after clicking the login button instead of opening the next page – Akash Jun 30 '18 at 13:22
  • that may be, but we cannot read minds. We can't help you unless we can see the code so we can figure out what is causing your crash. – Timothy Winters Jun 30 '18 at 13:24
  • ok, first, that is not of much use there. edit your main question and add the code there. Second, is that all of the code? – Timothy Winters Jun 30 '18 at 13:26
  • `Could not find method onClick(View) ` . You have added onClick inside xml . and you have not created this method in your Activity. – ADM Jun 30 '18 at 13:27
  • Possible duplicate of [How to solve error: Could not find method onClick(View) in a parent or ancestor Context for android:onClick](https://stackoverflow.com/questions/37136574/how-to-solve-error-could-not-find-method-onclickview-in-a-parent-or-ancestor) – ADM Jun 30 '18 at 13:28
  • **never** set click listeners in XML, or you will end up with this error. – Vladyslav Matviienko Jun 30 '18 at 13:59

1 Answers1

0

As @ADM mentioned in a comment to your “answer” @Akash your onClick method does not have the correct onClick method signature hence the java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context exception.

Drew MacInnis
  • 8,267
  • 1
  • 22
  • 18