我正在开发简单的 android 应用程序来获取用户的偏好,但设置活动没有出现
这是主要活动
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean OnOptionItemSelected(MenuItem item)
{
if(item.getItemId()==R.id.action_settings)
{
Toast.makeText(this,"Hi there",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,SetPreferenceActivity.class);
startActivity(intent);
}
return true;
}
}
这是偏好活动
public class Preference extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefernces);
}
}
偏好 xml 在这里
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="Private server information"
android:title="Local Server">
<EditTextPreference
android:key="ip"
android:summary="Please Enter Ip"
android:title="Configure ip" />
<EditTextPreference
android:key="port"
android:summary="Enter port number"
android:title="Configure port"
/>
</PreferenceCategory>
<PreferenceCategory
android:summary="Soft Settings"
android:title="Soft Settings">
<EditTextPreference
android:key="appname"
android:summary="AppName Configuration"
android:title="AppName"
/>
<EditTextPreference
android:key="apikey"
android:summary="Api Key configuration"
android:title="API Key"
/>
</PreferenceCategory>
</PreferenceScreen>
这是活动xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back_ground"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="95dp"
android:background="#002039"
android:onClick="onClick"
android:text="@string/scancode"
android:textColor="#ffffff" />
</RelativeLayout>
这是 android manifest.xml
<xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.mandy.splitsecond1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.mandy.splitsecond1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.mandy.splitsecond1.Preference"
android:label="@string/title_activity_preference" >
</activity>
<activity
android:name="com.android.mandy.splitsecond1.SetPreferenceActivity">
</activity>
这里是 setPreferenceActivity
public class SetPreferenceActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new Preference())
.commit();
}
}