1

我已经关注了其他一些问题并将其拼凑起来得到这个:

    public class FbFPS extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fbhtf);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter
                .createFromResource(this, R.array.spagesarray,
                        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner s = (Spinner) findViewById(R.id.spages);

        s.setAdapter(adapter);

        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                // Display Selected option
                if (parent.getItemAtPosition(pos).toString()
                        .equals("Under 16s Reccommended Settings")) {
                    Intent i = new Intent(getApplicationContext(),
                            FbU16RS.class);
                    startActivity(i);
                    finish();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                // Display Selected option
                if (parent.getItemAtPosition(pos).toString()
                        .equals("Recommended Privacy Settings")) {
                    Intent i = new Intent(getApplicationContext(), FbRS.class);
                    startActivity(i);
                    finish();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

    }
}

还有两个类似的活动,但它们指向剩余的两个活动。所以基本上是这样的:

s.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            // Display Selected option
            if (parent.getItemAtPosition(pos).toString()
                    .equals("Recommended Privacy Settings")) {
                Intent i = new Intent(getApplicationContext(), FbRS.class);
                startActivity(i);
                finish();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    });

每次活动两次。清单中提到了它们,并且 xml 是正确的。虽然它打开了最后一个选项,但确实打开了另一个选项,但它真的很奇怪。当在那个开始的时候,它不会打开任何......

我哪里错了?我也愿意接受任何更简单的方法。:) 提前致谢。

编辑:

好的,我已经在所有 3 个活动上进行了更改,现在它似乎在我选择的任何内容上都打开了相同的活动。这是代码:

    public class FbRS extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fbhtf);

        final Intent iFbHTF = new Intent(FbRS.this, FbHTF.class);
        final Intent iFbU16RS = new Intent(FbRS.this, FbU16RS.class);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter
                .createFromResource(this, R.array.spagesarray,
                        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner s = (Spinner) findViewById(R.id.spages);

        s.setAdapter(adapter);

        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                if (parent.getItemAtPosition(pos).toString()
                        .equals("Finding Privacy Settings")) {
                    startActivity(iFbHTF);
                    finish();
                } else if (parent.getItemAtPosition(pos).toString()
                        .equals("Under 16s Recommended Privacy Settings")) {
                    startActivity(iFbU16RS);
                    finish();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

    }
}

这里还有清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.timmo.isp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.timmo.isp.Home"
            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.timmo.isp.FbHTF"
            android:label="@string/titleFbHTF"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
        <activity
            android:name="com.timmo.isp.FbU16RS"
            android:label="@string/titleFbU16RS"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
        <activity
            android:name="com.timmo.isp.FbRS"
            android:label="@string/titleFbRS"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
        <activity
            android:name="com.timmo.isp.FYMNK"
            android:label="@string/titlefymnk"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
    </application>

</manifest>

我认为它到达那里希望感谢 AndroidPenguin 和 kongkea 的帮助。

4

2 回答 2

0

下面是您想要的代码,尽管括号可能太多或太少。基本上只有一个 onitemselectedlistener 会被调用。所以把两者放在一个。

public class FbFPS extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fbhtf);

ArrayAdapter<CharSequence> adapter = ArrayAdapter
        .createFromResource(this, R.array.spagesarray,
                android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner s = (Spinner) findViewById(R.id.spages);

s.setAdapter(adapter);

s.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        // Display Selected option
        if (parent.getItemAtPosition(pos).toString()
                .equals("Under 16s Reccommended Settings")) {
            Intent i = new Intent(getApplicationContext(),
                    FbU16RS.class);
            startActivity(i);
            finish();
        } else if (parent.getItemAtPosition(pos).toString()
                .equals("Recommended Privacy Settings")) {
            Intent i = new Intent(getApplicationContext(), FbRS.class);
            startActivity(i);
            finish();
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }

});

}
}

如果您愿意,您还可以使用 id/位置编号和开关和大小写来清理您的代码。

于 2012-12-09T02:45:31.910 回答
0

FBFPS 活动

Intent i = new Intent(FBFPS.this, FbU16RS.class);

在上面的 onClickListener 上。像 FBFPS 活动一样做。设置活动的当前名称 Intent i = new Intent(your current activity name.this, FbRS.class);

在 Manifest 中,在应用程序标签中添加所有 Activity 类名称

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ToDoListActivity" // Main activity
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".your activity name">
    </activity>
    <activity 
        android:name=".your activity name>
    </activity>

</application>

于 2012-12-09T02:48:51.960 回答