Search code examples
javaandroideclipsesdklogcat

Supplied button click method is not being found


I am a newbie, and plus this is my first post I hope i am clear with my problem and i really hope that someone can help me.

First of all, i am trying to make a simple page with some buttons, one of those button need to redirect me to a webpage. But my problem was that i didnt knew how to make a redirect-button so i have take a look at this one: How to open a website when a Button is clicked in Android application?

As i was following i seriously had many trouble. i even didnt knew where to put them. But after googleing and reading on the Android site i was able to do this, yay :)

Anyway, i still had litte problems, this line gave it to be exact.

public class LinkButtons extends Activity {

i changed this to:

class LinkButtons extends Activity {

and i was able to go, but not sure if it was a good idea...

i made the buttons on the XML but it gives me a warning (something about need to be hardcoded?) I ignored this one because i still was able to see the button with the text.

when i start the program it works fine, but when i touch this button (with need to redirect me to a webpage) the program stops for few seconds, and exits, and says "the process (my program) has stopped"

and in the Logcat i see this:

11-07 00:22:06.580: D/ActivityThread(30706): setTargetHeapUtilization:0.25
11-07 00:22:06.580: D/ActivityThread(30706): setTargetHeapIdealFree:8388608
11-07 00:22:06.580: D/ActivityThread(30706): setTargetHeapConcurrentStart:2097152
11-07 00:22:06.780: D/libEGL(30706): loaded /system/lib/egl/libEGL_adreno200.so
11-07 00:22:06.800: D/libEGL(30706): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-07 00:22:06.810: D/libEGL(30706): loaded /system/lib/egl/libGLESv2_adreno200.so
11-07 00:22:06.820: I/Adreno200-EGL(30706): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.1_RB1.04.01.01.45.000_msm8625_JB_REL_2.0.3.1_Merge_release_AU (Merge)
11-07 00:22:06.820: I/Adreno200-EGL(30706): Build Date: 03/28/13 Thu
11-07 00:22:06.820: I/Adreno200-EGL(30706): Local Branch: 
11-07 00:22:06.820: I/Adreno200-EGL(30706): Remote Branch: m/jb_rel_2.0.3.1
11-07 00:22:06.820: I/Adreno200-EGL(30706): Local Patches: NONE
11-07 00:22:06.820: I/Adreno200-EGL(30706): Reconstruct Branch: NOTHING
11-07 00:22:06.850: D/OpenGLRenderer(30706): Enabling debug mode 0
11-07 00:22:16.850: D/AndroidRuntime(30706): Shutting down VM
11-07 00:22:16.850: W/dalvikvm(30706): threadid=1: thread exiting with uncaught exception (group=0x41eb6438)
11-07 00:22:16.850: E/AndroidRuntime(30706): FATAL EXCEPTION: main
11-07 00:22:16.850: E/AndroidRuntime(30706): java.lang.IllegalStateException: Could not find a method goToSu(View) in the activity class com.example.[need to remove this part, sorry].FullscreenActivity for onClick handler on view class android.widget.Button with id 'button_su'
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View$1.onClick(View.java:3667)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View.performClick(View.java:4191)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View$PerformClick.run(View.java:17229)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.os.Handler.handleCallback(Handler.java:615)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.os.Looper.loop(Looper.java:137)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.app.ActivityThread.main(ActivityThread.java:4963)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.reflect.Method.invoke(Method.java:511)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at dalvik.system.NativeStart.main(Native Method)
11-07 00:22:16.850: E/AndroidRuntime(30706): Caused by: java.lang.NoSuchMethodException: goToSu [class android.view.View]
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at java.lang.Class.getMethod(Class.java:915)
11-07 00:22:16.850: E/AndroidRuntime(30706):    at android.view.View$1.onClick(View.java:3660)
11-07 00:22:16.850: E/AndroidRuntime(30706):    ... 11 more

What should i do now? or how do i fix this?

i appreciate any reply.

PS: I have added that .java code in my current one, this is okay right? Or did i had to make another .java file?

P.P.S: Like i said, im a very newbie, if you gonna tell a advanced story, please explain... Thanks,

EDIT:

Here is the code, Java and XML;

on the top of java;

    package com.example.[removed];
import com.example.[removed].util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

then

     * The instance of the {@link SystemUiHider} for this activity.
 */
private SystemUiHider mSystemUiHider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
            .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                // Cached values.
                int mControlsHeight;
                int mShortAnimTime;

                class LinkButtons extends Activity {

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

                    public void goToSo (View view) {
                        goToUrl ( "https://stackoverflow.com/");
                    }

                    public void goToSu (View view) {
                        goToUrl ( "http://superuser.com/");
                    }

                    private void goToUrl (String url) {
                        Uri uriUrl = Uri.parse(url);
                        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
                        startActivity(launchBrowser);
                    }
                }

                @Override
                @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                public void onVisibilityChange(boolean visible) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

and then XML

            <Button
            android:id="@+id/button_so"
            style="?buttonBarButtonStyle"
            android:layout_width="104dp"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:clickable="true"
            android:cursorVisible="true"
            android:linksClickable="true"
            android:onClick="goToSo"

            android:text="StackOverflow" />

        <Button
            android:id="@+id/button_su"
            style="?buttonBarButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoLink="web"
            android:clickable="true"
            android:onClick="goToSu"
            android:text="SuperUser" >
        </Button>
    </LinearLayout>

</FrameLayout>

Solution

  • It looks like you declared your onClick() on your Button in xml which is fine. But you are missing the method that you declared in android:onClick="goToSu". Add this method in your Activity.

    public void goTuSu(View v)
    {
         // your code
    }
    

    In the link you posted, you were obviously following the answer by Alain Pannetier who has this method in that answer

    public void goToSu (View view) {
        goToUrl ( "http://superuser.com/");
    }
    

    Edit

    Also from the answer you linked to is this method

     private void goToUrl (String url) {
        Uri uriUrl = Uri.parse(url);
        Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
        startActivity(launchBrowser);
    }
    

    which is called from goToSu(). goToUrl() is a custom method...not part of the Android framework.

    You can see how the onClick() should be implemented through xml in the Button Docs