Search code examples
javaandroidstringdatesettext

Trying to get date from Calendar... app crashes


What I want?: Write a method to:

  • 1st: Get date.

  • 2nd: Make it a String.

  • 3rd: setText(String) into an EditText.

What I've done:

private void setToday() {
    String dateToday = "";
    DateFormat df = new SimpleDateFormat("dd MM yyyy, HH:mm");
    dateToday = df.format(Calendar.getInstance().getTime());
    EditText title = (EditText) findViewById(R.id.Title);
    dateToday = df.toString();
    title.setText(dateToday);
}

Also:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

EditText & Button:

 <EditText
            android:id="@+id/Title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Title" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Today"
            android:onClick="setToday"/>

What should it do?: In Utopia, this should get date, focus on EditText and put date into EditText.

What it does?: Button clicked = App crashes.

This is a brand new app. I've tryed even only:

private void setToday() {
        EditText title = (EditText) findViewById(R.id.Title);
        title.setText("HOY");
    }

It still crashes. I'm doing something wrong, but cannot figure what it is.

EDIT: I managed to find logcat:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method setToday(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton

at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)

at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)

at android.view.View.performClick(View.java:4232)

at android.view.View$PerformClick.run(View.java:17298)

at android.os.Handler.handleCallback(Handler.java:615)

at android.os.Handler.dispatchMessage(Handler.java:92)

at android.os.Looper.loop(Looper.java:137)

at android.app.ActivityThread.main(ActivityThread.java:4921)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

at dalvik.system.NativeStart.main(Native Method)

Thanks for help.

PS: Here is all my code:

Java

package com.example.android.notastxt;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class MainActivity extends AppCompatActivity {

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

    private void setToday() {
        String dateToday = "HOY";
        DateFormat df = new SimpleDateFormat("dd MM yyyy, HH:mm");
        dateToday = df.format(Calendar.getInstance().getTime());
        EditText title = (EditText) findViewById(R.id.Title);
        dateToday = df.toString();
        title.setText(dateToday);
    }

}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.example.android.notastxt.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/Title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Title" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Today"
            android:onClick="setToday"/>
    </LinearLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="start"
        android:hint="Write" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save" />


</LinearLayout>

Solution

  • I deserve all your laughts. This was simply newbie mistake.

    Changed

    private void setToday() {
    

    For

    public void setToday(View View) {
    

    Voilà! It worked flawlessly.

    I used code from @Basil Bourque from here: https://stackoverflow.com/a/46415908/8662218

    DateFormat df = new SimpleDateFormat("dd MM yyyy, HH:mm");
    String dateToday = df.format(Calendar.getInstance().getTime());
    EditText title = (EditText) findViewById(R.id.Title);
    title.setText(dateToday);
    

    Thank you, Basil.

    Problem solved.