0

Hey everyone I'm new to android programming so this should be pretty basic.

No matter what I do the android emulator crashes. To show how little it takes for this to happen, I've opened a new project called MyApplication, added a button to my xml file, and ran the button. After I run this button I get "Unfortunately MyApplication has stopped" When it should just allow me to click constantly. Ive already tried changing the ram, making sure the emulator is on host GPU, and making sure i opened a brand new application while changing no defaults and adding nothing but a simple button. Why is this happening?

Button code:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Here"
    android:onClick="onClick"
    android:id="@+id/button"
     />

Logcat:

12-05 13:06:35.459 1974-1974/com.example.owner.myapplication I/art: Not late-enabling -Xcheck:jni (already on)
12-05 13:06:35.459 1974-1974/com.example.owner.myapplication I/art: Late-enabling JIT
12-05 13:06:35.461 1974-1974/com.example.owner.myapplication I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
12-05 13:06:35.488 1974-1974/com.example.owner.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.owner.myapplication-1/lib/x86
12-05 13:06:35.567 1974-1989/com.example.owner.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-05 13:06:35.568 1974-1974/com.example.owner.myapplication D/: HostConnection::get() New Host Connection established 0xab795310, tid 1974
12-05 13:06:35.607 1974-1989/com.example.owner.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
12-05 13:06:35.641 1974-1989/com.example.owner.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
12-05 13:06:35.641 1974-1989/com.example.owner.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabf217e0, error=EGL_SUCCESS
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication D/AndroidRuntime: Shutting down VM

12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime: Process: com.example.owner.myapplication, PID: 1974
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime: 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 'button'
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:310)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:269)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.view.View.performClick(View.java:5198)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-05 13:06:36.999 1974-1974/com.example.owner.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-05 13:06:38.800 1974-1974/? I/Process: Sending signal. PID: 1974 SIG: 9
gustavohenke
  • 40,997
  • 14
  • 121
  • 129
  • "Why is this happening?" -- presumably, there is a bug in your code. Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Dec 05 '15 at 18:14

2 Answers2

2

Just make sure your activity has the following method:

public void onClick (View view) {
  // your logic
}
gustavohenke
  • 40,997
  • 14
  • 121
  • 129
0

Try to write somw logic inside the Onclick function Unless you write a code in the onclick function the emulator will show you an error As you have not defined what needs to be done Onclicking the button Try writing a code of toast or new Intent in the onclick so that something actually happens and the OS knows what shall be done after clicking the button