我正在尝试创建一个首选项页面,一旦选择了某个项目,它就会转到该应用程序的首选项页面。当我点击该项目时,它不起作用。有人可以帮助我找到如何使这项工作?这是代码:
首选项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);
}
}
}
主要活动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>
答案 0 :(得分:0)
我认为这是因为你将错误的上下文传递给了意图:
而不是:
case R.id.item1: Intent i = new Intent(this, UserSettingActivity.class);
写下这个:
case R.id.item1: Intent i = new Intent(yourActivityName.this, UserSettingActivity.class);