无法使用 PreferenceActivity 中的按钮调用方法

Unable to call methods with a button in PreferenceActivity

这是我第一次使用PreferenceActivity

我为设置屏幕做了一个简单的 activity,当我在布局中设置一个按钮并为该按钮设置 onClickListener 时,没有任何操作,也没有任何操作错误。

我只想通过按下按钮将用户送回主屏幕。是否有错误或我遗漏了某些设置?

神秘的部分是 xml onClick 和直接方法调用都不起作用。没有操作,没有错误,它甚至不显示 Toast(为调试而更改时)。有没有人实际使用过 PreferenceActivity 并使用过按钮之类的小部件?

这是我的设置屏幕示例 activity

    public class Settings extends PreferenceActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);

//This button doesn't work 
            Button b = (Button)findViewById(R.id.setupid);
            b.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {

             Intent i = new Intent(Settings.this,HomeActivity.class);
            startActivity(i);

                }
            });
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();



        }



        public static class MyPreferenceFragment extends PreferenceFragment
        {
            @Override
            public void onCreate(final Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.preferences);
            }


        }

    }

这是 Settings.java

的 xml 布局
<?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="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3498db"
        android:gravity="center"

      >


        <Button
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="Set"

            android:id="@+id/setupid" />
    </LinearLayout>
</LinearLayout>

您一开始不必使用 Fragment。您所要做的就是使用 addPreferencesFromResource(R.xml.preferences);在 setContents 视图下。

 public class Settings extends PreferenceActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);
           //It is depricated but it works 
            addPreferencesFromResource(R.xml.preferences);

            Button b = (Button)findViewById(R.id.setupid);
            b.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {

             Intent i = new Intent(Settings.this,HomeActivity.class);
            startActivity(i);

                }
            });
            //getFragmentManager().beginTransaction().replace(android.R.id.content, //new MyPreferenceFragment()).commit();



        }


    }