Reputation: 2115
I want to create very simple ListView
but unfortunately every time app crasehes.
This is my code:
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity implements OnItemClickListener {
public static String[] daysArray = {"Ponedeljek", "Torek", "Sreda", "Četrtek", "Petek", "Sobota", "Nedelja"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, daysArray);
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.i("Info", "CLICKED");
}
}
Layout main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
EDIT:
02-18 14:56:51.549: D/dalvikvm(2618): GC_FOR_ALLOC freed 68K, 6% free 3096K/3280K, paused 17ms, total 17ms
02-18 14:56:51.561: I/dalvikvm-heap(2618): Grow heap (frag case) to 4.202MB for 1127532-byte allocation
02-18 14:56:51.565: D/dalvikvm(2618): GC_FOR_ALLOC freed 2K, 5% free 4195K/4384K, paused 4ms, total 4ms
02-18 14:56:51.581: D/AndroidRuntime(2618): Shutting down VM
02-18 14:56:51.585: W/dalvikvm(2618): threadid=1: thread exiting with uncaught exception (group=0xa4d89b20)
02-18 14:56:51.585: E/AndroidRuntime(2618): FATAL EXCEPTION: main
02-18 14:56:51.585: E/AndroidRuntime(2618): Process: com.example.listsstudy, PID: 2618
02-18 14:56:51.585: E/AndroidRuntime(2618): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listsstudy/com.example.listsstudy.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.os.Handler.dispatchMessage(Handler.java:102)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.os.Looper.loop(Looper.java:136)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-18 14:56:51.585: E/AndroidRuntime(2618): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 14:56:51.585: E/AndroidRuntime(2618): at java.lang.reflect.Method.invoke(Method.java:515)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-18 14:56:51.585: E/AndroidRuntime(2618): at dalvik.system.NativeStart.main(Native Method)
02-18 14:56:51.585: E/AndroidRuntime(2618): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.Activity.setContentView(Activity.java:1929)
02-18 14:56:51.585: E/AndroidRuntime(2618): at com.example.listsstudy.MainActivity.onCreate(MainActivity.java:19)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.Activity.performCreate(Activity.java:5231)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-18 14:56:51.585: E/AndroidRuntime(2618): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-18 14:56:51.585: E/AndroidRuntime(2618): ... 11 more
Upvotes: 0
Views: 147
Reputation: 244
need to use
android:id="@android:id/list"
instead of:
android:id="@+id/listView"
If you use ListActivity. Or just try to use Activity, not ListActivity
EDIT: my code; Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...
And in my class:
public class AnyNameHere extends ListActivity {
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.listview );
// do what you want here;
}
and it's working :)
Upvotes: 2
Reputation: 9700
When you want to entend ListActivity
to create your activity, as MainActivity
, then your ListView
xml must be identified the android:id
with list
as follows...
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Upvotes: 0
Reputation: 8870
You're ListView has the wrong ID. From the documentation:
To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code)
Upvotes: 2
Reputation: 157437
you are extending ListActivity
. Your ListView
has to have
android:id="@android:id/list"
as id.
Upvotes: 3