0

The start of the activity is causing my app to crash. It isn't the firts OnClick on the app.

NOT WORKING

 public void  start_motora(View view){
     Intent intent = new Intent(getApplicationContext(), MotorActivity.class);
     startActivity(intent);
 }

WORKING

public void  start_app(View view){
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);  
    startActivity(intent);
}

NOT WORKING XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/white">

    <ImageButton
        android:onClick="start_motora"
        android:id="@+id/motor"
        android:layout_width="0dp"
        android:layout_height="54dp"
        android:layout_marginStart="44dp"
        android:layout_marginTop="32dp"
        android:background="@drawable/purplebutton"
        android:src="@drawable/ic_face_white"
        app:layout_constraintEnd_toStartOf="@+id/alfabeto"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text11" />
 </androidx.constraintlayout.widget.ConstraintLayout>

WORKING XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity"
    android:background="#ffd500">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/logo_sam"
        android:onClick="start_app"
        android:outlineAmbientShadowColor="@color/yellow"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        style="?android:attr/borderlessButtonStyle"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Console Error

The error. I've changed the onClick name and the Activity name, but it's the same problem.

2021-11-03 14:34:03.659 29506-29506/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 29506
java.lang.IllegalStateException: Could not find method teste(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button2'
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)
    at android.view.View.performClick(View.java:7448)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28305)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Help please. The error does not link to some error in my code, i'm so confused

2 Answers2

0

You must register your MotorActivity activity in your manifest.

<activity android:name=".MotorActivity" />
Ji Sungbin
  • 891
  • 1
  • 10
  • 19
0
  1. As i can see the above code, if you are using Activity then you don't need to use getApplicationContext() instead of this you should use this.ActivityName or only this because Context is define for their specific reason like example if you are using fragment then you can use context from onAttach and for Apdater you can use application context.

  2. Please share your error without error i am not sure what is the reason of crash in your app.

for reference check this answer : Android button onClick changing activity cause app to crash

and for context description :- https://medium.com/@banmarkovic/what-is-context-in-android-and-which-one-should-you-use-e1a8c6529652

Apps Maven
  • 1,314
  • 1
  • 4
  • 17