Elven
Elven

Reputation: 310

displaying date & time

I want to display date and time separately, but application crashes instead. If i remove time side of it, then it doesn't crash. But I really need to display date on one place and time separitely on the other place.

    // date & time \\
    // date
    dateatm = (TextView) findViewById(R.id.date);
    Calendar currentDate = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd");
    String dateNow = formatter.format(currentDate.getTime());
    dateatm.setText(dateNow);
    // time
    timeatm = (TextView) findViewById(R.id.time);
    Calendar currentTime = Calendar.getInstance();
    SimpleDateFormat värk = new SimpleDateFormat("HH:mm:ss");
    String timeNow = värk.format(currentTime.getTime());
    timeatm.setText(timeNow);

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="Tere " android:id="@+id/tvTere"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:textSize="20dp"></TextView>
    <TextView android:id="@+id/date"
    android:text="kuupäev" android:layout_width="fill_parent"
    android:textSize="20dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center"></TextView>
    <TextView android:id="@+id/time"
    android:text="kellaaeg" android:layout_width="fill_parent"
    android:textSize="20dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center"></TextView>

Error in

08-10 16:22:13.614: ERROR/AndroidRuntime(531): FATAL EXCEPTION: main
08-10 16:22:13.614: ERROR/AndroidRuntime(531): java.lang.RuntimeException: Unable to start activity ComponentInfo{viimane.voimalus/viimane.voimalus.MainStuff}: java.lang.NullPointerException
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.os.Looper.loop(Looper.java:123)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at java.lang.reflect.Method.invokeNative(Native Method)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at java.lang.reflect.Method.invoke(Method.java:507)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at dalvik.system.NativeStart.main(Native Method)
08-10 16:22:13.614: ERROR/AndroidRuntime(531): Caused by: java.lang.NullPointerException
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at viimane.voimalus.MainStuff.onCreate(MainStuff.java:52)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-10 16:22:13.614: ERROR/AndroidRuntime(531):     ... 11 more

Upvotes: 0

Views: 444

Answers (2)

Tom
Tom

Reputation: 208

The field "timeatm" is null at at line 52. Meaning findViewById() did not find the component "time" in the layout. Sure you have the right XML layout file set with setContentView(), and setContentView() is called before the code above?

Note (in general) that findViewById() does NOT fail if it does not find a component - it just returns null. In the interest of catching errors ("fail early"), ALWAYS check the result of findViewById for null and throw an exception. Your code will fail in either case, but using this method you will know what is going on (i.e. you messed up your layout, chose the wrong layout, take your pick -.- in essence, you are guarding against future errors by yourself or other maintainers of your code).

Upvotes: 1

Danail
Danail

Reputation: 10583

Things look right on the first glance.

As the other users proposed, this should be connected to the xml file.

You maybe have some error there - either you misspelled some id (not highly possible, especially if you don't have other xml layout files), or the views aren't TextViews.

BTW, where do you place this source - after calling setContentView() ? This maybe the error - if you call it before, it won't work, the view won't be inflated.

Upvotes: 0

Related Questions