Note that there are some explanatory texts on larger screens.

plurals
  1. POTo Do List Example
    text
    copied!<p>I am new to Android App Development and have a small problem.</p> <p>I have tried to follow an example to create a to do list app. However it doesn't work even though I have copied it exactly.</p> <p>It shows no errors when I run it however, a blank screen appears and after a few seconds it closes and says "Unfortunately, ToDoList has stopped."</p> <p>Does anyone know what this problem might be?</p> <p><strong>Code</strong></p> <pre><code> package com.paad.todolist; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView myListView = (ListView)findViewById(R.id.ListView); final EditText myEditText = (EditText) findViewById(R.id.EditText); final ArrayList&lt;String&gt; todoItems = new ArrayList&lt;String&gt;(); final ArrayAdapter&lt;String&gt; aa; aa = new ArrayAdapter&lt;String&gt; (this,android.R.layout.simple_list_item_1,todoItems); myListView.setAdapter(aa); myEditText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER)) { todoItems.add(0, myEditText.getText().toString()); aa.notifyDataSetChanged(); myEditText.setText(""); return true; } return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } </code></pre> <p><strong>Layout XML File</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;EditText android:id="@+id/EditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/addItemHint" android:contentDescription="@string/addItemContentDescription" /&gt; &lt;ListView android:id="@+id/ListView" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Log:</p> <pre><code>02-10 20:03:53.850: D/AndroidRuntime(24220): Shutting down VM 02-10 20:03:53.850: W/dalvikvm(24220): threadid=1: thread exiting with uncaught exception (group=0x40f0a2a0) 02-10 20:03:53.850: E/AndroidRuntime(24220): FATAL EXCEPTION: main 02-10 20:03:53.850: E/AndroidRuntime(24220): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.todolist/com.paad.todolist.MainActivity}: java.lang.NullPointerException 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.access$600(ActivityThread.java:140) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.os.Handler.dispatchMessage(Handler.java:99) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.os.Looper.loop(Looper.java:137) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.main(ActivityThread.java:4898) 02-10 20:03:53.850: E/AndroidRuntime(24220): at java.lang.reflect.Method.invokeNative(Native Method) 02-10 20:03:53.850: E/AndroidRuntime(24220): at java.lang.reflect.Method.invoke(Method.java:511) 02-10 20:03:53.850: E/AndroidRuntime(24220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 02-10 20:03:53.850: E/AndroidRuntime(24220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 02-10 20:03:53.850: E/AndroidRuntime(24220): at dalvik.system.NativeStart.main(Native Method) 02-10 20:03:53.850: E/AndroidRuntime(24220): Caused by: java.lang.NullPointerException 02-10 20:03:53.850: E/AndroidRuntime(24220): at com.paad.todolist.MainActivity.onCreate(MainActivity.java:30) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.Activity.performCreate(Activity.java:5206) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 02-10 20:03:53.850: E/AndroidRuntime(24220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 02-10 20:03:53.850: E/AndroidRuntime(24220): ... 11 more </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload