Reputation: 19
While I try to run it shows the app has stopped and I got the following error in the logcat:
12-18 21:47:44.545 26245-26245/com.example.bloodpressuremonitoring1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bloodpressuremonitoring1, PID: 26245
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bloodpressuremonitoring1/com.example.bloodpressuremonitoring1.MainActivity}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.ImageView
at com.example.bloodpressuremonitoring1.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
Here is my MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static int SPLASH_SCREEN = 5000;
//Variables
Animation topAnim, bottomAnim;
ImageView image;
TextView logo, slogan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//Animations
topAnim = AnimationUtils.loadAnimation(this, R.anim.top_animation);
bottomAnim = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);
//Hooks
image = findViewById(R.id.imageView);
logo = findViewById(R.id.textView);
slogan = findViewById(R.id.textView2);
image.setAnimation(topAnim);
logo.setAnimation(bottomAnim);
slogan.setAnimation(bottomAnim);
}
}
Here is my activity_main.xml:
<ImageView
android:layout_width="266dp"
android:layout_height="312dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/blood_pressure"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:fontFamily="@font/bilbo_swash_caps"
android:gravity="center"
android:text="Blood Pressure Monitoring"
android:textSize="35sp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:fontFamily="sans-serif-smallcaps"
android:gravity="center"
android:text="Monitor your blood pressure anytime"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Please help me, thanks a lot...
Upvotes: 1
Views: 3694
Reputation: 1068
You have accidently added imageview id to Constraintlayout id. just change that it will work fine. Error: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.ImageView
Upvotes: 0
Reputation: 216
Your ImageView doesn't have an id
<ImageView
android:id="@+id/imageView"
...
/>
Maybe you put @+id/imageView
on the ConstraintLayout by accident?
Upvotes: 2