设置首选项活动未启动



我正在开发简单的安卓应用程序,用于从用户那里获取首选项,但设置活动没有出现

这是主要活动

    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>

这是安卓清单.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>

这是设置首选项活动

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

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

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

}

相关内容

  • 没有找到相关文章

最新更新