设置首选项活动未启动

时间:2013-10-08 13:09:54

标签: android xml preferences

我正在开发简单的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;
                    }

                }

这是prefence活动

public class Preference extends PreferenceFragment 
        {

            @Override
            public void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.prefernces);


            }


        }

Prefernce 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();
        }

    } 

1 个答案:

答案 0 :(得分:0)

在SetPreferenceActivity中,您应该从PreferenceActivity扩展而不是从PreferenceFragment扩展。 像这样:

public class SetPreferenceActivity extends PreferenceActivity implements
            OnSharedPreferenceChangeListener, OnPreferenceClickListener { // if needed

}