Q: Android App crashes when clicking on button I created two activities, one MainActivity and the other is CookieActivity.
I try to go to the cookie activity by clicking on the image from the main one, but the app crashes immediately after clicking.
I am new to android development.
Thanks for helping me with that!
my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.applicationelharemelhazami">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ApplicationElharemElhazami">
        <activity android:name=".CookieActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The mainActivity:
package com.example.applicationelharemelhazami;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageButton play;

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

        this.play = (ImageButton) findViewById(R.id.play);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent otherActivity = new Intent(getApplicationContext(), CookieActivity.class);
                startActivity(otherActivity);
                finish();
            }
        });
    }
}

activity_main.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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name_wahed"
        android:textColor="@color/red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.525"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.702" />

    <ImageButton
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/graven_logo"
        tools:layout_editor_absoluteX="11dp"
        tools:layout_editor_absoluteY="16dp"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

CookieActivity :
package com.example.applicationelharemelhazami;

//import android.support.v7.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class CookieActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cookie);

    }
}

Logcat:
2021-01-08 13:08:33.424 2997-2997/? I/elharemelhazam: Late-enabling -Xcheck:jni
2021-01-08 13:08:33.444 2997-2997/? E/elharemelhazam: Unknown bits set in runtime_flags: 0x8000
2021-01-08 13:08:33.570 2997-2997/com.example.applicationelharemelhazami I/Perf: Connecting to perf service.
2021-01-08 13:08:33.580 2997-2997/com.example.applicationelharemelhazami I/FeatureParser: can't find curtana.xml in assets/device_features/,it may be in /vendor/etc/device_features
2021-01-08 13:08:33.588 2997-2997/com.example.applicationelharemelhazami E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
2021-01-08 13:08:33.579 2997-2997/com.example.applicationelharemelhazami W/elharemelhazami: type=1400 audit(0.0:235986): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=25646 scontext=u:r:untrusted_app:s0:c65,c257,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0
2021-01-08 13:08:33.628 2997-2997/com.example.applicationelharemelhazami D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.example.applicationelharemelhazami activity: com.example.applicationelharemelhazami.MainActivity@38f037c
2021-01-08 13:08:33.630 2997-2997/com.example.applicationelharemelhazami D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.example.applicationelharemelhazami activity: com.example.applicationelharemelhazami.MainActivity@38f037c
2021-01-08 13:08:33.650 2997-2997/com.example.applicationelharemelhazami W/elharemelhazam: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-01-08 13:08:33.651 2997-2997/com.example.applicationelharemelhazami W/elharemelhazam: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-01-08 13:08:33.658 2997-2997/com.example.applicationelharemelhazami D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.example.applicationelharemelhazami activity: com.example.applicationelharemelhazami.MainActivity@38f037c
2021-01-08 13:08:33.660 2997-2997/com.example.applicationelharemelhazami I/chatty: uid=10321(com.example.applicationelharemelhazami) identical 1 line
2021-01-08 13:08:33.663 2997-2997/com.example.applicationelharemelhazami D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.example.applicationelharemelhazami activity: com.example.applicationelharemelhazami.MainActivity@38f037c
2021-01-08 13:08:33.715 2997-3039/com.example.applicationelharemelhazami I/AdrenoGLES: QUALCOMM build                   : ed77089, I0c4fc4bb0e
    Build Date                       : 12/24/19
    OpenGL ES Shader Compiler Version: EV031.27.05.03
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
2021-01-08 13:08:33.715 2997-3039/com.example.applicationelharemelhazami I/AdrenoGLES: Build Config                     : S P 8.0.12 AArch64
2021-01-08 13:08:33.718 2997-3039/com.example.applicationelharemelhazami I/AdrenoGLES: PFP: 0x016ee187, ME: 0x00000000
2021-01-08 13:08:33.735 2997-3039/com.example.applicationelharemelhazami W/Gralloc3: mapper 3.x is not supported
2021-01-08 13:08:34.731 2997-2997/com.example.applicationelharemelhazami I/Timeline: Timeline: Activity_launch_request time:443838815
2021-01-08 13:08:34.759 2997-2997/com.example.applicationelharemelhazami W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@176ca0d
2021-01-08 13:08:34.772 2997-2997/com.example.applicationelharemelhazami D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.example.applicationelharemelhazami activity: com.example.applicationelharemelhazami.CookieActivity@fd2fa09
2021-01-08 13:08:34.773 2997-2997/com.example.applicationelharemelhazami D/ForceDarkHelper: updateByCheckExcludeList: pkg: com.example.applicationelharemelhazami activity: com.example.applicationelharemelhazami.CookieActivity@fd2fa09
2021-01-08 13:08:34.785 2997-2997/com.example.applicationelharemelhazami D/AndroidRuntime: Shutting down VM
2021-01-08 13:08:34.787 2997-2997/com.example.applicationelharemelhazami E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.applicationelharemelhazami, PID: 2997
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applicationelharemelhazami/com.example.applicationelharemelhazami.CookieActivity}: android.view.InflateException: Binary XML file line #2 in com.example.applicationelharemelhazami:layout/activity_cookie: Binary XML file line #2 in com.example.applicationelharemelhazami:layout/activity_cookie: Error inflating class android.support.constraint.ConstraintLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: android.view.InflateException: Binary XML file line #2 in com.example.applicationelharemelhazami:layout/activity_cookie: Binary XML file line #2 in com.example.applicationelharemelhazami:layout/activity_cookie: Error inflating class android.support.constraint.ConstraintLayout
     Caused by: android.view.InflateException: Binary XML file line #2 in com.example.applicationelharemelhazami:layout/activity_cookie: Error inflating class android.support.constraint.ConstraintLayout
     Caused by: java.lang.ClassNotFoundException: android.support.constraint.ConstraintLayout
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:454)
        at android.view.LayoutInflater.createView(LayoutInflater.java:819)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.applicationelharemelhazami.CookieActivity.onCreate(CookieActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
2021-01-08 13:08:34.787 2997-2997/com.example.applicationelharemelhazami E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.ConstraintLayout" on path: DexPathList[[zip file "/data/app/com.example.applicationelharemelhazami-12cjP0_3sHnyiYT3ttbaHw==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.applicationelharemelhazami-12cjP0_3sHnyiYT3ttbaHw==/lib/arm64, /system/lib64, /product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
            ... 26 more



A: Try to use the same type of views.
In your XML declaration you add an ImageButton, and in your Activity you're trying to get some ImageView...

A: replace your private ImageView play; to private ImageButton play; and replace  this.play = (ImageView) findViewById(R.id.play); to  play=findViewById(R.id.play); And That's It.

A: Problem is here:
You are using ImageButton in your activity_main.xml and you initialize as ImageView in your MainActivity.java
Solution:
Initialize ImageButton as ImageButton in your MainActivity.java  OR replace your ImageButton with ImageView in your activity_main.xml

A: The problem is solved! I simply created a new project and redo the same thing and somehow it worked!
Thanks for all who answered, I really apreciate your effort.
