1

All of my code was working properly until I attempted to add in the date. When i did that, the applications crashes on Launch. Here is what I have so far:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<!-- Header -->



<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:textColor="#FFFFFF"
    android:gravity="center"
    android:layout_marginTop="20dp"
    android:id="@+id/Header"
    android:text=""
/>

<!-- Name -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:id="@+id/Name"
    android:text="" 
/>

<!-- Project Name -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:id="@+id/ProjectName"
    android:text=""    
/>

<!-- Digital Clock -->

<DigitalClock
    android:id="@+id/DigitalClock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:text="" 
/>

<!-- Calendar -->

<TextView
    android:id="@+id/stringCalendar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:text="" 
/>

<!-- End of Program -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:background="#000000"
    android:gravity="center"
    android:id="@+id/EndOfProgram"
    android:text=""
/>

</LinearLayout>

MainActivity.java:

package com.example.d1project1;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.widget.DigitalClock;

import android.widget.TextView;

@SuppressWarnings({ "deprecation", "unused" })
public class MainActivity extends Activity {
    //Declare GUI objects

    TextView stringHeader;
    TextView stringName;        
    TextView stringProject;
    TextView digitalClock1;
    TextView stringEnd;

    TextView stringCalendar;
    TextView stringDisplay;
    DigitalClock DigitalClock;

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

        Init_GUI();
        DigitalClock();
        Nonevent_Event();
    }

    private void DigitalClock() {
        //DigitalClock
        DigitalClock = (DigitalClock)findViewById(R.id.DigitalClock);
    }
    private void Nonevent_Event() {
        String stringDate = "";

        Date_Class.date_time(stringCalendar);

        stringCalendar.setText(stringDate);
    }

    //method to link GUI items to code 
    private void Init_GUI() {
        stringHeader = (TextView)findViewById(R.id.Header);
        stringName = (TextView)findViewById(R.id.Name);
        stringProject = (TextView)findViewById(R.id.ProjectName);
        stringEnd = (TextView)findViewById(R.id.EndOfProgram);

        stringHeader.setText("Day 1 Project 1 - Travis Finlan");
        stringName.setText("Travis Finlan");
        stringProject.setText("Day 1 Project 1");
        stringEnd.setText("End of Program");
    }
}

Date_Class.java:

package com.example.d1project1;

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

import android.widget.DigitalClock;
import android.widget.TextClock;
import android.widget.TextView;

public class Date_Class {
    public static String date_time(TextView stringCalendar) {
        //Calendar
        Calendar c = Calendar.getInstance();
        System.out.println("Current time => " + c.getTime());

        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        String formattedDate = df.format(c.getTime());

        return formattedDate;
    }
}

Also, if you see any errors, please let me know. I can't seem to figure out why it keeps crashing on me. I just want it to show the current date along with the current time. I have to have all the methods in the non-instantiated class, so they have to be pulled over.

LogCat Information

02-19 17:02:17.785: DEBUG/AndroidRuntime(2095): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
02-19 17:02:17.785: DEBUG/AndroidRuntime(2095): CheckJNI is ON
02-19 17:02:17.885: DEBUG/AndroidRuntime(2095): --- registering native functions ---
02-19 17:02:17.915: INFO/jdwp(2095): Ignoring second debugger -- accepting and dropping
02-19 17:02:18.305: DEBUG/AndroidRuntime(2095): Shutting down VM
02-19 17:02:18.305: DEBUG/dalvikvm(2095): Debugger has detached; object registry had 1 entries
02-19 17:02:18.327: INFO/AndroidRuntime(2095): NOTE: attach of thread 'Binder Thread #3' failed
02-19 17:02:18.635: DEBUG/AndroidRuntime(2103): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
02-19 17:02:18.635: DEBUG/AndroidRuntime(2103): CheckJNI is ON
02-19 17:02:18.736: DEBUG/AndroidRuntime(2103): --- registering native functions ---
02-19 17:02:18.755: INFO/jdwp(2103): Ignoring second debugger -- accepting and dropping
02-19 17:02:19.175: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.d1project1/.MainActivity }
02-19 17:02:19.195: INFO/ActivityManager(59): Start proc com.example.d1project1 for activity com.example.d1project1/.MainActivity: pid=2109 uid=10036 gids={}
02-19 17:02:19.225: DEBUG/AndroidRuntime(2103): Shutting down VM
02-19 17:02:19.225: DEBUG/dalvikvm(2103): Debugger has detached; object registry had 1 entries
02-19 17:02:19.275: INFO/dalvikvm(2103): JNI: AttachCurrentThread (from ???.???)
02-19 17:02:19.275: INFO/AndroidRuntime(2103): NOTE: attach of thread 'Binder Thread #3' failed
02-19 17:02:19.365: INFO/jdwp(2109): Ignoring second debugger -- accepting and dropping
02-19 17:02:19.679: INFO/System.out(2109): Current time => Wed Feb 19 17:02:19 CST 2014
02-19 17:02:19.686: DEBUG/AndroidRuntime(2109): Shutting down VM
02-19 17:02:19.686: WARN/dalvikvm(2109): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109): FATAL EXCEPTION: main
02-19 17:02:19.696: ERROR/AndroidRuntime(2109): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.d1project1/com.example.d1project1.MainActivity}: java.lang.NullPointerException
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.os.Looper.loop(Looper.java:123)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at java.lang.reflect.Method.invoke(Method.java:521)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at dalvik.system.NativeStart.main(Native Method)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109): Caused by: java.lang.NullPointerException
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at com.example.d1project1.MainActivity.Nonevent_Event(MainActivity.java:44)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at com.example.d1project1.MainActivity.onCreate(MainActivity.java:32)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-19 17:02:19.696: ERROR/AndroidRuntime(2109):     ... 11 more
02-19 17:02:19.706: WARN/ActivityManager(59):   Force finishing activity com.example.d1project1/.MainActivity
02-19 17:02:19.706: WARN/ActivityManager(59):   Force finishing activity com.android.mms/.ui.ConversationList
02-19 17:02:20.206: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{4a4c5dd0 com.example.d1project1/.MainActivity}
02-19 17:02:24.827: INFO/Process(2109): Sending signal. PID: 2109 SIG: 9
02-19 17:02:24.846: INFO/ActivityManager(59): Process com.example.d1project1 (pid 2109) has died.
02-19 17:02:26.416: DEBUG/dalvikvm(233): GC_EXPLICIT freed 4119 objects / 277728 bytes in 123ms
02-19 17:02:31.137: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{4a4c5dd0 com.example.d1project1/.MainActivity}
02-19 17:02:39.466: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
5
  • 5
    Please post the output of logcat so that we can see the cause of the crash.
    – Ameen
    Feb 19, 2014 at 22:21
  • @Ameen How do i get output of logcat? I went into it and There is nothing there.
    – Travis
    Feb 19, 2014 at 22:33
  • 1
    Typically, your IDE (Eclipse, Android Studio, etc.) print the logcat output for you. You can also manually get it via the adb command, see developer.android.com/tools/help/logcat.html
    – Ameen
    Feb 19, 2014 at 22:45
  • @Ameen I have added it in
    – Travis
    Feb 19, 2014 at 23:04
  • Henry's response seems to point to the issue. stringCalendar is null (you probably want to set its value in your InitGUI method)
    – Ameen
    Feb 19, 2014 at 23:06

1 Answer 1

1

You never set the field stringCalendar in class MainActivity. Therefore you get a NullPointerException from this line:

stringCalendar.setText(stringDate);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.