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>
CodePudding user response:
You must register your MotorActivity
activity in your manifest.
<activity android:name=".MotorActivity" />
CodePudding user response:
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.
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