简体   繁体   中英

My android studio code built successful but output app crashing

Here's my MainActivity's XML file:

<?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="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.47"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.877" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="404dp"
        android:layout_height="331dp"
        android:layout_marginStart="3dp"
        android:layout_marginTop="94dp"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="199dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ImageContrastCheck"
        tools:srcCompat="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>

And here's the Java file:

package com.wavedevelopers.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;

import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

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

        ImageView imageView;

        Calendar cal = Calendar.getInstance();

        Date now_date = null;
        cal.setTime(now_date);

        imageView = (ImageView)findViewById(R.id.content);

        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)
        {
            imageView.setImageResource(R.drawable.a1);
        }
        else if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY)
        {
            imageView.setImageResource(R.drawable.a2);
        }

    }
}

The Above code built successfully but out not displaying in the emulator, verify the above code next

NEED CODE CHANGES :

If(cal.get(Calendar.DAY_OF_WEEK) == CALENDAR.MONDAY)

This condition checks the DAY_OF_WEEK , but I want to check both DATE_OF_MONTH and MONTH with the local date.

Example: If local calender date is equal to given date, display the image

you are setting a null date into your cal object Date now_date = null; change this line to Date now_date = new Date();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM