MrPool MrPool - 9 days ago 6
Android Question

App crashes when a button is used to invoke a new activity

I'm using a button to go to an about page in my application, but whenever the button is clicked the app crashes. There are no gradle builds errors.

In the parent class, I've used this code. This piece of code has worked for me in the past without any issues. Whenever I hover over the button 2, it states that 'button2(android.view.View)' is never used, I've imported android view.View above.

public void button2(View view){
Intent intent = new Intent(this, prepo.class);
startActivity(intent);
}


I've then used this in the Android manifest.

<activity
android:name=".prepo"
android:label="About"
android:parentActivityName=".BitRates">
</activity>


And my xml in the parent class is as follows.

<Button
android:text="About"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView6"
android:layout_centerHorizontal="true"
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Borderless"
android:background="?android:attr/colorBackground"
android:backgroundTint="?android:attr/colorBackground"
android:onClick="button2 (BitRates)" />


I've used this before in developing applications and it has worked, without any errors or crashes.

Edit:

Error logs.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.name, PID: 3101
java.lang.IllegalStateException: Could not find method button2 (BitRates)(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: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)
Application terminated.


Solved, changes in the xml from

android:onClick="button2 (BitRates)"


to this

android:onClick="button2" />

Answer

Update your button xml code to this :

<Button
    android:text="About"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView6"
    android:layout_centerHorizontal="true"
    android:id="@+id/button2"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:background="?android:attr/colorBackground"
    android:backgroundTint="?android:attr/colorBackground"
    android:onClick="button2" />

What above code is doing is it will trigger the method button2 of your activity. Earlier it was searching for following method name: button2 (BitRates) in your acitvity

Comments