2013-10-08 131 views
0

我開發簡單的Android應用程序從用戶獲得prefernces但設置活動沒有出現設置偏好活動沒有開始

這裏是主要活動

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

    } 

回答

0

在你SetPreferenceActivity,您應該從PreferenceActivity不PreferenceFragment延伸。 事情是這樣的:

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

}