Hi Wrox,
Your Book "Professional Android Application Development" is really very interesting..
and for starters you have explained clearly all the basics required to understand Android Development..
I'm very very new to Android Development..
I have some prob with ToDolist..
I am just posting code snippet here..
Can you plz help me out where i'm going wrong...
Code:
package com.todo;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.view.View.OnKeyListener;
public class ToDoList extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.myListView);
final EditText myEditText = (EditText) findViewById(R.id.myEditText);
final ArrayList<String> todoItems = new ArrayList<String>();
final ArrayAdapter<String> aryAdapter;
aryAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, todoItems);
myListView.setAdapter(aryAdapter);
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());
aryAdapter.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
}
}
and xml file is as follows:
Code:
<?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" >
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint=""
android:text="@string/txt_list" />
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
when i execute this in emulator i get a msg as:
"Unexpectedly TO-Do has stopped"
and logcat shows as follows:
Code:
01-20 12:34:06.627: D/dalvikvm(610): Not late-enabling CheckJNI (already on)
01-20 12:34:11.697: D/AndroidRuntime(610): Shutting down VM
01-20 12:34:11.697: W/dalvikvm(610): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-20 12:34:11.818: E/AndroidRuntime(610): FATAL EXCEPTION: main
01-20 12:34:11.818: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.todo/com.todo.ToDoList}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:137)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-20 12:34:11.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 12:34:11.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 12:34:11.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 12:34:11.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 12:34:11.818: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method)
01-20 12:34:11.818: E/AndroidRuntime(610): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-20 12:34:11.818: E/AndroidRuntime(610): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.Activity.setContentView(Activity.java:1835)
01-20 12:34:11.818: E/AndroidRuntime(610): at com.todo.ToDoList.onCreate(ToDoList.java:22)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.Activity.performCreate(Activity.java:4465)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 12:34:11.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-20 12:34:11.818: E/AndroidRuntime(610): ... 11 more
01-20 12:34:15.877: I/Process(610): Sending signal. PID: 610 SIG: 9
so what might be the actual reason for this prob..??
Waiting for ur reply...
Regards,
Mounika