1

I am trying to create an application wherein a date value is selected and passed through. The code on the date is selection is as follows:

datePicker = (DatePicker)findViewById(R.id.datePicker);
    Integer dtYear = datePicker.getYear();
    Integer dtMonth = datePicker.getMonth();
    Integer dtDate = datePicker.getDayOfMonth();
    StringBuilder sb=new StringBuilder();
    sb.append(dtYear.toString()).append("-").append(dtMonth.toString()).append("-").append(dtDate.toString()).append(" 00:00:00");
    String dobStr=sb.toString();

    Intent intent = new Intent(this, MoodDiary.class);

    intent.putExtra(EXTRA_MESSAGE, dobStr);
    startActivity(intent);

This goes through to the next page, where I want to have the date selected displayed at the top. My problem is that if I use

TextView textView = new TextView(this);
    textView.setTextSize(20);
    textView.setText(message);

setContentView(textView);

then I can see the date but not the other features on the page, and if I use

setContentView(R.layout.activity_mood_diary);

the label that holds the date value reverts back to just saying 'hello world' i.e. the default text. I have tried to get around this through the following:

TextView t = (TextView)findViewById(R.id.changeText);
    //t.setText(message); //this line causes the error

But I then get a null pointer error, which I think may have something to do with the code not being able to find the given text view, or it not being able to find changeText. ChangeText is the id given to the text view label I am trying to make hold the date, so I am not sure why it cannot be found.

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/changeText" />

I am new to Android studio, so any help would be much appreciated.

EDIT: Complete activity mood diary.xml, as requested:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.katkin.stressguard.MoodDiary">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/changeText" />

<TextView android:text="Mood Rating" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changeText"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp"
    android:id="@+id/textView2" />

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingBar"
    android:layout_alignTop="@+id/textView2"
    android:layout_alignStart="@+id/ratingBar2" />

<TextView android:text="Wellbeing"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@+id/ratingBar"
    android:layout_alignParentStart="true"
    android:layout_marginTop="39dp"
    android:id="@+id/textView3" />

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingBar2"
    android:layout_alignTop="@+id/textView3"
    android:layout_alignStart="@+id/editText" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Minutes Exercised"
    android:id="@+id/textView4"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="41dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/button"
    android:layout_above="@+id/textView4"
    android:layout_toEndOf="@+id/textView4"
    android:layout_marginStart="39dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="-"
    android:id="@+id/button2"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="0"
    android:id="@+id/textView5"
    android:layout_alignBottom="@+id/textView4"
    android:layout_alignEnd="@+id/ratingBar2"
    android:layout_below="@+id/button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Time Up"
    android:id="@+id/timeUpText"
    android:layout_alignTop="@+id/editText"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/ratingBar2"
    android:layout_alignEnd="@+id/textView4" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText2"
    android:layout_alignTop="@+id/editText"
    android:layout_alignStart="@+id/button" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Time to bed"
    android:id="@+id/textView6"
    android:layout_marginTop="41dp"
    android:layout_below="@+id/editText"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText3"
    android:layout_alignTop="@+id/textView6"
    android:layout_alignStart="@+id/editText" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText4"
    android:layout_alignTop="@+id/editText3"
    android:layout_alignStart="@+id/button" />

Que
  • 19
  • 4
  • Could you show the activity_mood_diary.xml? – Niko Adrianus Yuwono Oct 30 '15 at 00:29
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – George Oct 30 '15 at 00:51
  • You need to show us the complete activity_mood_diary.xml. Common causes of this issue are when the id (changeText) has been incorrectly assigned to multiple controls in the same layout, or the id exists in one configuration of the layout (eg. res/layout/activity_mood_diary.xml), but not in the other configurations (eg. res/layout-land/activity_mood_diary.xml) – adelphus Oct 30 '15 at 01:41

0 Answers0