0

我正在尝试创建一个首选项页面,一旦选择了一个项目,它就会转到应用程序的首选项页面。当我单击该项目时,它不起作用。请有人可以帮我找出如何使这项工作?这是代码:

偏好 Java 文件:

public class UserSettingActivity extends PreferenceActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        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);
        }
    }

}

Main Activity Java 文件特定代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Goes back to the home screen
    case R.id.item1:  Intent i = new Intent(this, UserSettingActivity.class);
    startActivity(i);

首选项 XML 文件:

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


<EditTextPreference
    android:title="EditText"
    android:key="name"
    android:summary="Enter your name" />

<CheckBoxPreference 
    android:title="Vibrate"
    android:defaultValue="true"
    android:key="vibrateCheckbox"
    android:summary="Turn vibrate on or off"/>

</PreferenceScreen>
4

1 回答 1

0

我认为这是因为您将错误的上下文传递给意图:

而不是这个:

case R.id.item1:  Intent i = new Intent(this, UserSettingActivity.class);

写这个:

case R.id.item1:  Intent i = new Intent(yourActivityName.this, UserSettingActivity.class);
于 2013-03-30T15:44:32.310 回答