Why is my Android application failing after starting for a couple seconds?

package org.profdev.todo_list;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoList extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Inflates your view
    setContentView(R.layout.activity_to_do_list);

    //Get references to UI Widgets
    ListView myListView = (ListView) findViewById(R.id.myListView);
    final EditText myEditText = (EditText) findViewById(R.id.myEditText);

    //Create ArrayList<> of To Do items
    final ArrayList<String> todoItems = new ArrayList<String>();
    //Create ArrayAdapter to bind the Array to ListView
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
    //Bind ArrayAdapter to ListView
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new OnKeyListener()
    {
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
                if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
                {
                    todoItems.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                }

            return false;   
            }           
        });
    }
}

activity_to_do_list.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText 
        android:id="@+id/myEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/new_to_do" />

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

I am a new android developer, I'm doing a tutorial out of the book "Professional Development 4 Android" and have the same code that is in the book. My code is not working for some reason. I have no compiler errors but in the Emulator, after starting the application I get the dialog box of "Unfortunately, Todo_List has stopped."

11-13 23:03:06.124: D/AndroidRuntime(844): Shutting down VM
11-13 23:03:06.124: W/dalvikvm(844): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-13 23:03:06.253: E/AndroidRuntime(844): FATAL EXCEPTION: main
11-13 23:03:06.253: E/AndroidRuntime(844): android.app.SuperNotCalledException: Activity {org.profdev.todo_list/org.profdev.todo_list.ToDoList} did not call through to super.onCreate()
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2177)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.os.Looper.loop(Looper.java:137)
11-13 23:03:06.253: E/AndroidRuntime(844):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-13 23:03:06.253: E/AndroidRuntime(844):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 23:03:06.253: E/AndroidRuntime(844):  at java.lang.reflect.Method.invoke(Method.java:525)
11-13 23:03:06.253: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-13 23:03:06.253: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 23:03:06.253: E/AndroidRuntime(844):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 121

Answers (3)

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Add

super.onCreate(savedInstanceState);

in your onCreate()

like:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Inflates your view
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);

Upvotes: 3

Reshurum
Reshurum

Reputation: 98

You need to call super.onCreate(savedInstanceState) in onCreate as the exception states.

Upvotes: 0

C--
C--

Reputation: 16558

Before doing anything in the onCreate, you should call the super class's onCreate. The first line in your onCreate() method should be,

super.onCreate(savedInstanceState);

It will fix the problem for now. You may read this question and its answers to see why this has to be done.

Upvotes: 2

Related Questions