【问题标题】:My android studio code built successful but output app crashing [duplicate]我的android studio代码构建成功但输出应用程序崩溃[重复]
【发布时间】:2021-11-02 07:16:00
【问题描述】:

这是我的 MainActivity 的 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="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>

这是 Java 文件:

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);
        }

    }
}

上面的代码编译成功但是模拟器没有显示,验证上面的代码 下一个

需要更改代码:

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

此条件检查DAY_OF_WEEK,但我想同时检查DATE_OF_MONTH 和MONTH 与本地日期。

示例:如果本地日历日期等于给定日期,则显示图像

【问题讨论】:

  • My android studio code built successful but output app crashing 编译时错误和运行时错误有很大的区别,你应该了解堆栈跟踪以及如何读取错误来帮助你调试

标签: java android validation


【解决方案1】:

您在 cal 对象中设置了一个空日期 Date now_date = null; 将此行更改为 Date now_date = new Date();

【讨论】:

  • 感谢您的回复,需要代码更改:If(cal.get(Calendar.DAY_OF_WEEK) == CALENDAR.MONDAY) 此条件检查 DAY_OF_WEEK,但我想同时检查 DATE_OF_MONTH 和 MONTH当地日期。示例:如果本地日历日期等于给定日期,则显示图像
  • 我没有完全理解你的意思,但是如果你想在一个 if 语句中检查两个条件,你应该在你的条件之间使用 && : if("a" == "a" && "b" == "b")
猜你喜欢
  • 2018-12-27
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2022-11-11
  • 2023-01-27
  • 1970-01-01
相关资源
最近更新 更多