Search code examples
androidlistadaptersimpleadapter

SimpleAdapter with layout and SQLite crashing on startup Android


Im trying to make an app in android with an list!

well im getting the data i want from my database and now im trying to put in in an listview

i tried following an tutorial and that worked.

now im building an app for myself and my app crashes on startup here is the code of my activity

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        this.setContentView(R.layout.activity_stats);
        setContentView(R.layout.activity_stats);
        ListView listContent = (ListView)findViewById(R.id.listShit);

        ArrayList<HashMap<String, String>> shitList =  dbTools.getAllShits();
          dbTools.getAllShits();


        if(shitList.size()!=0) {



            ListAdapter adapter = new SimpleAdapter( stats.this,shitList, R.id.shit_entry, new String[] { "shitId","earned"}, new int[] {R.id.shitId, R.id.earned});


            setListAdapter(adapter);
        }

the logcat

8-11 21:29:47.718  29617-29617/jmsbrk.crappapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: jmsbrk.crappapp, PID: 29617
    java.lang.RuntimeException: Unable to start activity ComponentInfo{jmsbrk.crappapp/jmsbrk.crappapp.stats}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
            at android.app.ActivityThread.access$800(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ListActivity.onContentChanged(ListActivity.java:243)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1973)
            at jmsbrk.crappapp.stats.onCreate(stats.java:36)
            at android.app.Activity.performCreate(Activity.java:5389)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
            at android.app.ActivityThread.access$800(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

Solution

  • If you're extending ListActivity, Android by default tries to search for a Listview with id "@android:id/list". Make sure you are setting this.

    A basic example would be:

        <ListView android:id="@android:id/list"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"/>