Mohammed Shamshid Mohammed Shamshid - 6 months ago 18
Android Question

Button click force closes the app

I'm new in Android Development. I guess this is nooby question.
Q: I inserted 2 buttons to open 2 links and there were no code errors found in Android Studio but the app force closes on the action of click of the button. Any idea?

package com.domain.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class LinkButtons extends Activity {

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

public void goToBlue (View view) {
goToUrl ( "http://www.google.com");
}

public void goToRed (View view) {
goToUrl ( "http://www.facebook.com");
}

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

}


 

08-25 01:31:12.598 16635-16635/com.domain.app D/AndroidRuntime﹕ Shutting down VM
08-25 01:31:12.600 16635-16635/com.domain.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.domain.app, PID: 16635
java.lang.IllegalStateException: Could not find a method goToRed(View) in the activity class com.domain.app.MainActivity for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'button_Red'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: goToRed [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:4000)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


 

<Button
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="30dp"
android:text="RED"
android:background="@drawable/button_red"
android:layout_gravity="left|bottom"
android:autoLink="web"
android:clickable="true"
android:id="@+id/button_Red"
android:onClick="goToRed"
android:textColor="#ffffffff"
android:textSize="15dp" />


<Button
android:layout_width="106dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginRight="30dp"
android:background="@drawable/button_blue"
android:layout_gravity="right|bottom"
android:text="BLUE"
android:autoLink="web"
android:clickable="true"
android:id="@+id/button_Blue"
android:onClick="goToBlue"
android:textColor="#ffffffff"
android:textSize="15dp" />

Answer

From the LogCat, you are using this xml in MainActivity.java and your goToRed() method is in LinkButtons.java... this is why he can't find the method.

Comments