简体   繁体   English

在“首选项”屏幕的底部添加一个按钮

[英]Add a button to the bottom of Preferences Screen

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. 尽管@stealthcopter的评论内容(由@Andrew检查答案)在这个问题中如何向PreferenceScreen添加按钮 ,但我无法在活动的底部显示按钮。

The application .apk will crash with the following error: Unfortunately your_app has stopped working! 应用程序.apk将崩溃并出现以下错误: 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: 这是崩溃的活动java类:

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资源:

<?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? 能否请您解释导致应用程序崩溃的冲突是什么?

You have to include a ListView with the id @android:id/list like it says in the question you linked to (See Answer ). 你必须在你链接的问题中包含一个id为@android:id/list的ListView(参见答案 )。

From your android layout file (R.Layout.Preferences) remove your <ListView> and put this in it's place. 从您的Android布局文件(R.Layout.Preferences)中删除您的<ListView>并将其放在它的位置。

<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' 从你发布的LogCat: 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. 编辑:只是澄清了ListView上的id属性。 From Android XML Layouts . 来自Android XML布局

The syntax for an ID, inside an XML tag is: XML标记内的ID语法是:

android:id="@+id/my_button" 机器人: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. 字符串开头的at符号(@)表示XML解析器应解析并扩展ID字符串的其余部分,并将其标识为ID资源。 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). 加号(+)表示这是一个新的资源名称,必须创建并添加到我们的资源中(在R.java文件中)。 There are a number of other ID resources that are offered by the Android framework. Android框架提供了许多其他ID资源。 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包命名空间,如下所示:

android:id="@android:id/empty" 机器人:ID = “@机器人:ID /空”

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. 有了android包命名空间,我们现在引用android.R资源类中的ID,而不是本地资源类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 屏幕底部1/3处的对齐按钮 - Align button at the bottom 1/3 of the screen 在页面底部添加结帐按钮 - Add checkout button at bottom of page 在布局和滚动视图底部添加按钮 - Add button at the bottom of layout and scrollview 如何将简单的首选项屏幕添加到android输入法编辑器应用 - How to add a simple preferences screen to an android input method editor app 如何在屏幕底部添加具有回收器视图的按钮。 这样按钮就不会隐藏回收器视图的最后一个元素 - How can I add button at the bottom of the screen having a recycler View. so that the button don't hide the last element of recycler view 当内容较小时,将按钮固定在屏幕底部;如果内容足够大以固定整个屏幕,则将按钮固定在内容的底部 - Fix the button at the bottom of the screen when content is small and at the bottom of the content if content is large enough to take the whole screen 如何在borderlayout右下角的java应用程序按钮中添加一个按钮? - How to Add a button to java application button right bottom of the borderlayout? 尝试将新的TextView添加到屏幕底部并将其堆叠 - Trying to add new TextView to Bottom of Screen and have them Stacked 如何在屏幕底部添加按钮并将文本编辑为页面的第三列? - how to add buttons at the bottom of the screen and edit text as the third column of the page? 如何在Android App的底角向Google Play商店添加按钮? - How to add button to Google Play store in bottom corner of Android App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM