Discussion:
[android-developers] App crashed when I press my button...
Michael Maus
2016-01-29 19:07:38 UTC
Permalink
I am just starting to learn Android Studio and am in now way a programmer.

I have been following this tutorial to create a button to go to a new
activity, but get an error when the button is pressed and the app crashed.

*Tutorial I followed:*


*Error I see in Android Monitor log:*
01-29 14:05:23.867 2225-2225/com.xplore_scuba.xplorescuba I/art: Not late-enabling
-Xcheck:jni (already on)
01-29 14:05:23.950 2225-2225/com.xplore_scuba.xplorescuba W/System:
ClassLoader referenced unknown path:
/data/app/com.xplore_scuba.xplorescuba-2/lib/x86
01-29 14:05:24.124 2225-2243/com.xplore_scuba.xplorescuba D/OpenGLRenderer:
Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-29 14:05:24.174 2225-2243/com.xplore_scuba.xplorescuba I/OpenGLRenderer:
Initialized EGL, version 1.4
01-29 14:05:24.264 2225-2243/com.xplore_scuba.xplorescuba W/EGL_emulation:
eglSurfaceAttrib not implemented
01-29 14:05:24.264 2225-2243/com.xplore_scuba.xplorescuba W/OpenGLRenderer:
Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7dfd60, error=EGL_SUCCESS
01-29 14:05:30.529 2225-2225/com.xplore_scuba.xplorescuba D/AndroidRuntime:
Shutting down VM
01-29 14:05:30.530 2225-2225/com.xplore_scuba.xplorescuba E/AndroidRuntime:
FATAL EXCEPTION: main

Process: com.xplore_scuba.xplorescuba, PID: 2225

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

at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)

at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)

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)
01-29 14:05:33.364 2225-2228/com.xplore_scuba.xplorescuba W/art: Suspending
all threads took: 21.239ms
01-29 14:05:33.665 2225-2225/com.xplore_scuba.xplorescuba I/Process:
Sending signal. PID: 2225 SIG: 9



I have my files on GitHub if anyone would like to dig into it. Its a small
project right now with a single button and two activities.
https://github.com/mouse51180/NewActivityTest


I dont know why the OnClick is having an issue and dont know what or where
to look to correct this. My suspicions are that something needs to be
changed or added to the MainActivity.java file.

The button itself has the "onClick" parameter set to "onButtonClick". Im
not sure is this might be were part of the problem is as well.

*MainActivity.java file:*

package com.xplore_scuba.xplorescuba;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;


public class MainActivity extends AppCompatActivity {

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

public void onButtonClick(View v)
{
if(v.getId() == R.id.btn_calendar)
{
Intent i = new Intent(MainActivity.this, Display.class);
startActivity(i);
}
}
}



Thanks in advance for any guidance you can provide.
--
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/afe7c84a-c47e-4783-8ec2-11e53aa2a0be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
TreKing
2016-01-29 23:07:45 UTC
Permalink
Post by Michael Maus
I dont know why the OnClick is having an issue and dont know what or where
to look to correct this. My suspicions are that something needs to be
changed or added to the MainActivity.java file.
The button itself has the "onClick" parameter set to "onButtonClick". Im
not sure is this might be were part of the problem is as well.
What you posted should work. This could easily be an Android bug.

An easy workaround would be to set the click handler yourself - which is
more obvious and explicit anyway. In onCreate:

findViewByID(R.id.btn_calendar).setOnClickListener(new OnClickListener()) {
...
}

-------------------------------------------------------------------------------------------------
TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
transit tracking app for Android-powered devices
--
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/CANCScggmWw8E7pOMD%2BCmri4O228wLOi1t8aWx-%3D92kmAp31RFw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
niks
2016-01-30 02:18:27 UTC
Permalink
Hi, I don't know what you did nor did I see the video, but try to do the
following things:

first, give an id to your button, let us call it *calender_button*.

The in the below example do it like this:



public class MainActivity extends AppCompatActivity {


Button calender; *(<--- this is step 1)*


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



//set the button, like *(<--- this is step 2)*


calender*= ** (Button) findViewById(R.id.**calender_button**);*


calender*.setOnClickListener(this); **(<--- this is step 3)*


}

public void onClick(View v) *(<--- step 4; I am using the case/scenario here so as to allow you to add more buttons later on; it will also work with just one button)*
{
if(v.getId())
{
case R.id.*calender_button*:
startActivity(new Intent(MainActivity.this, *yourNextActivity*.class));
break;
}
}
}



Try this and let me know how it went



΀η Παρασκευή, 29 ΙαΜουαρίου 2016 - 9:07:38 ÎŒ.ÎŒ. UTC+2, ο χρήστης Michael
Post by Michael Maus
I am just starting to learn Android Studio and am in now way a programmer.
I have been following this tutorial to create a button to go to a new
activity, but get an error when the button is pressed and the app crashed.
*Tutorial I followed:*
http://youtu.be/-xljI2_TRZg
*Error I see in Android Monitor log:*
01-29 14:05:23.867 2225-2225/com.xplore_scuba.xplorescuba I/art: Not late-enabling
-Xcheck:jni (already on)
/data/app/com.xplore_scuba.xplorescuba-2/lib/x86
01-29 14:05:24.124 2225-2243/com.xplore_scuba.xplorescuba
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-29 14:05:24.174 2225-2243/com.xplore_scuba.xplorescuba
I/OpenGLRenderer: Initialized EGL, version 1.4
eglSurfaceAttrib not implemented
01-29 14:05:24.264 2225-2243/com.xplore_scuba.xplorescuba
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7dfd60,
error=EGL_SUCCESS
01-29 14:05:30.529 2225-2225/com.xplore_scuba.xplorescuba
D/AndroidRuntime: Shutting down VM
01-29 14:05:30.530 2225-2225/com.xplore_scuba.xplorescuba
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xplore_scuba.xplorescuba, PID: 2225
java.lang.IllegalStateException: Could not find method ButtonClick(View)
in a parent or ancestor Context for android:onClick attribute defined on
view class android.support.v7.widget.AppCompatImageButton with id
'btn_calendar'
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
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)
Suspending all threads took: 21.239ms
Sending signal. PID: 2225 SIG: 9
I have my files on GitHub if anyone would like to dig into it. Its a
small project right now with a single button and two activities.
https://github.com/mouse51180/NewActivityTest
I dont know why the OnClick is having an issue and dont know what or where
to look to correct this. My suspicions are that something needs to be
changed or added to the MainActivity.java file.
The button itself has the "onClick" parameter set to "onButtonClick". Im
not sure is this might be were part of the problem is as well.
*MainActivity.java file:*
package com.xplore_scuba.xplorescuba;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v)
{
if(v.getId() == R.id.btn_calendar)
{
Intent i = new Intent(MainActivity.this, Display.class);
startActivity(i);
}
}
}
Thanks in advance for any guidance you can provide.
--
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/485eac89-28af-4e6a-8868-cb99d75c5614%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
niks
2016-01-30 02:51:13 UTC
Permalink
Hi Michael,
look at my first answer I corrected your MainActivity ;-)
--
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/12b3504e-7f9b-4ce6-b022-f77bff62ea50%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
niks
2016-01-30 03:01:59 UTC
Permalink
I also forgot to mention:

I ASSUME that you have added both activities to your manifest!!!
Otherwise, when clicking your button and the next activity is about to be
lunched your app will crash!
Post by niks
Hi Michael,
look at my first answer I corrected your MainActivity ;-)
--
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+***@googlegroups.com.
To post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/588c1e07-987c-47c9-abc4-3c1d02f73a27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...