3

Despite the content of the comment of @stealthcopter (check answer made by @Andrew) in this question How to add a button to PreferenceScreen, I am not able to visualise a button in the bottom part of my activity.

The application .apk will crash with the following error: Unfortunately your_app has stopped working!

Here is the stack trace from the crash:

05-25 21:26:39.431: I/dalvikvm(1405): threadid=3: reacting to signal 3
05-25 21:26:39.462: I/dalvikvm(1405): Wrote stack traces to '/data/anr/traces.txt'
05-25 21:26:44.851: I/Process(1405): Sending signal. PID: 1405 SIG: 9
05-25 21:40:51.442: I/dalvikvm(1936): threadid=3: reacting to signal 3
05-25 21:40:51.601: I/dalvikvm(1936): Wrote stack traces to '/data/anr/traces.txt'
05-25 21:40:51.951: I/dalvikvm(1936): threadid=3: reacting to signal 3
05-25 21:40:52.001: I/dalvikvm(1936): Wrote stack traces to '/data/anr/traces.txt'
05-25 21:40:52.472: I/dalvikvm(1936): threadid=3: reacting to signal 3
05-25 21:40:52.561: I/dalvikvm(1936): Wrote stack traces to '/data/anr/traces.txt'
05-25 21:40:52.631: D/dalvikvm(1936): GC_CONCURRENT freed 172K, 4% free 9313K/9607K, paused 7ms+7ms
05-25 21:40:52.691: D/gralloc_goldfish(1936): Emulator without GPU emulation detected.
05-25 21:40:54.372: D/AndroidRuntime(1936): Shutting down VM
05-25 21:40:54.372: W/dalvikvm(1936): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-25 21:40:54.422: E/AndroidRuntime(1936): FATAL EXCEPTION: main
05-25 21:40:54.422: E/AndroidRuntime(1936): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.DVA_HLUI/com.DVA_HLUI.DVA_HLUIPrefsActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.widget.TabHost.setCurrentTab(TabHost.java:346)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.view.View.performClick(View.java:3511)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.view.View$PerformClick.run(View.java:14105)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.os.Handler.handleCallback(Handler.java:605)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.os.Looper.loop(Looper.java:137)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at java.lang.reflect.Method.invoke(Method.java:511)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at dalvik.system.NativeStart.main(Native Method)
05-25 21:40:54.422: E/AndroidRuntime(1936): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.preference.PreferenceActivity.onContentChanged(PreferenceActivity.java:944)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.Activity.setContentView(Activity.java:1835)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at com.DVA_HLUI.DVA_HLUIPrefsActivity.onCreate(DVA_HLUIPrefsActivity.java:15)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.Activity.performCreate(Activity.java:4465)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-25 21:40:54.422: E/AndroidRuntime(1936):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-25 21:40:54.422: E/AndroidRuntime(1936):     ... 18 more

Here is the activity java class that crashes:

public class DVA_HLUIPrefsActivity extends PreferenceActivity 
{
   @Override
   public void onCreate(Bundle savedInstanceState) 
   { 
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        setContentView(R.layout.preferences);
   }
}

Here is its layout file:

<?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">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_weight="1">
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Quit!" android:layout_weight="0.5"android:layout_gravity="center_vertical"
         />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Help" android:layout_weight="0.5" android:layout_gravity="center_vertical"
         />

    </LinearLayout>

</LinearLayout>

and finally this is the preferences xml resource:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <Preference android:key="about" android:title="@string/titleAbout" android:summary="@string/summaryAbout" />


</PreferenceScreen>

Can you please explain me what is the conflict that causes the application to crash?

Community
  • 1
  • 1
Matteo
  • 7,924
  • 24
  • 84
  • 129
  • Please add the stack trace from the crash. – Mattias Isegran Bergander May 25 '12 at 19:38
  • @Pulsar added the stack trace – Matteo May 25 '12 at 19:44
  • And in the stacktrace is the answer you are looking for. Someone else explained it too it seems. – Mattias Isegran Bergander May 25 '12 at 20:03
  • @Pulsar I'm a newbie to android and maybe I haven't understood well, but preferences aren't usually defined as I did in the preferences xml resource?What am I getting wrong?If I replace that with the code in the answer below there will be an eclipse error: `error: Error parsing XML: unbound prefix` – Matteo May 25 '12 at 20:12
  • 1
    Only thing you need to change in your code is that listed in that answer, the `android:id="@android:id/list"` attribute for your list view. – Mattias Isegran Bergander May 25 '12 at 20:20
  • @Pulsar +1..I got it, so the problem was not the preference xml resource but the preferences layout, having this id `"@+id/list"` instead of this `"@android:id/list"`. Anyway, what's the difference between the 2? – Matteo May 25 '12 at 20:25
  • 1
    `@+id/list` adds a new id to *your* resources named `list`, while uses an already defined id, in this case in android itself: `@android:id/list` – Mattias Isegran Bergander May 25 '12 at 20:28

1 Answers1

4

You have to include a ListView with the id @android:id/list like it says in the question you linked to (See Answer).

From your android layout file (R.Layout.Preferences) remove your <ListView> and put this in it's place.

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

That should solve the problem you were having.

From the LogCat you posted: Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'


Edit: Just some clarification of the id attribute on the ListView. From Android XML Layouts.

The syntax for an ID, inside an XML tag is:

android:id="@+id/my_button"

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:

android:id="@android:id/empty"

With the android package namespace in place, we're now referencing an ID from the android.R resources class, rather than the local resources class.

Community
  • 1
  • 1
javajavajavajavajava
  • 1,223
  • 2
  • 12
  • 20
  • in the [Answer](http://stackoverflow.com/questions/2697233/how-to-add-a-button-to-preferencescreen/3026922#3026922) it is written `preferences defined the usual way in res/xml/preferences.xml` which is exactly what I did with the tag – Matteo May 25 '12 at 20:07
  • Hey @Matteo Your R.xml.prefs looks fine, you don't need to change that. You just need to change the android:id of your ListView in the R.layout.preferences file. I see that you've changed the id of the ListView to "@+id/list"... It needs to be "@android:id/list". Let me know if this solves it for you – javajavajavajavajava May 25 '12 at 20:24
  • yeah that was the issue!!thks a lot for help. btw what is the difference between the 2 ways of writing the id? – Matteo May 25 '12 at 20:27
  • @Matteo Great! I'm glad that fixed it for you. I added some more detail to the Answer to explain why these are different. Basically, we don't want to create a NEW ListView, we just want to use the standard Android ListView that PreferenceScreen is expecting. – javajavajavajavajava May 25 '12 at 20:33
  • That was of great help, sorry I can't +2... ;D thks again, bye ;D – Matteo May 25 '12 at 20:35