Android 使用微调器切换到其他活动

Android 使用微调器切换到其他活动,android,android-intent,switch-statement,android-spinner,Android,Android Intent,Switch Statement,Android Spinner,我对Java完全陌生,所以我想向您道歉,并请您理解这个问题。 我正在尝试使用一个可以切换到其他活动的微调器。每当我运行emulator时,我都会收到一个错误消息,很遗憾,您的应用程序已停止运行。我是否需要在我要切换到的活动中添加以下代码以外的内容 public class LakeOptions extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(sav

我对Java完全陌生,所以我想向您道歉,并请您理解这个问题。 我正在尝试使用一个可以切换到其他活动的微调器。每当我运行emulator时,我都会收到一个错误消息,很遗憾,您的应用程序已停止运行。我是否需要在我要切换到的活动中添加以下代码以外的内容

public class LakeOptions extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lake);``

}
这是我的代码,来自附加到xml的活动,上面有我的微调器。是否需要向xml微调器标记添加任何内容?我再次为我的无知道歉

public class MainActivity extends ActionBarActivity {

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

    Spinner spinner = (Spinner) findViewById(R.id.spinner);

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

    spinner.setAdapter(adapter);

      class SpinnerActivity extends Activity implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view,
                                   int pos, long id) {
           switch(pos) {
               case 0:
                   Intent j = new Intent ( this, JasperOptions.class);
                   startActivity(j);
                   break;
               case 1:
                   Intent k = new Intent(this, LakeOptions.class);
                   startActivity(k);
                   break;
               case 2:
                   Intent l = new Intent(this, LaporteOptions.class);
                   startActivity(l);
                   break;
               case 3:
                   Intent n = new Intent(this, NewtonOptions.class);
                   startActivity(n);
                   break;
               case 4:
                   Intent p = new Intent(this, PorterOptions.class);
                   startActivity(p);
                   break;
           }
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }
    }

}


@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;

                }


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

    }
这是我的舱单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helpfinder.app" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.helpfinder.app.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.helpfinder.app.JasperOptions"
        android:label="JasperOptions">

    </activity>
    <activity
        android:name="com.helpfinder.app.JasperFood"
        android:label="JasperFood">
    </activity>
    <activity
        android:name="com.helpfinder.app.JasperHousing"
        android:label="JasperHousing">
    </activity>
    <activity
        android:name="com.helpfinder.app.JasperAssistance"
        android:label="JasperAssistance">
    </activity>
    <activity
        android:name="com.helpfinder.app.JasperOtherServices"
        android:label="JasperOtherServices">
    </activity>

    <activity
        android:name="com.helpfinder.app.LakeOptions"
        android:label="LakeOptions">
    </activity>
    <activity
        android:name="com.helpfinder.app.LakeFood"
        android:label="LakeFood">
    </activity>
    <activity
        android:name="com.helpfinder.app.LakeHousing"
        android:label="Lake County Homeless Shelters"
        >
    </activity>
    <activity
        android:name="com.helpfinder.app.LakeAssistance"
        android:label="LakeAssistance">
    </activity>
    <activity
        android:name="com.helpfinder.app.LakeOtherServices"
        android:label="LakeOtherServices">
    </activity>

    <activity
        android:name="com.helpfinder.app.LaporteOptions"
        android:label="LaporteOptions">
    </activity>
    <activity
        android:name="com.helpfinder.app.LaporteFood"
        android:label="LaporteFood">
    </activity>
    <activity
        android:name="com.helpfinder.app.LaporteHousing"
        android:label="LaporteHousing">
    </activity>
    <activity
        android:name="com.helpfinder.app.LaporteAssistance"
        android:label="LaporteAssistance">
    </activity>
    <activity
        android:name="com.helpfinder.app.LaporteOtherServices"
        android:label="LaporteOtherServices">
    </activity>
    +
    <activity
        android:name="com.helpfinder.app.NewtonOptions"
        android:label="NewtonOptions">
    </activity>
    <activity
        android:name="com.helpfinder.app.NewtonFood"
        android:label="NewtonFood">
    </activity>
    <activity
        android:name="com.helpfinder.app.NewtonHousing"
        android:label="NewtonHousing">
    </activity>
    <activity
        android:name="com.helpfinder.app.NewtonAssistance"
        android:label="NewtonAssistance">
    </activity>
    <activity
        android:name="com.helpfinder.app.NewtonOtherServices"
        android:label="NewtonOtherServices">
    </activity>

    <activity
    android:name="com.helpfinder.app.PorterOptions"
    android:label="PorterOptions">
</activity>
    <activity
        android:name="com.helpfinder.app.PorterFood"
        android:label="PorterFood">
    </activity>
    <activity
        android:name="com.helpfinder.app.PorterHousing"
        android:label="PorterHousing">
    </activity>
    <activity
        android:name="com.helpfinder.app.PorterAssistance"
        android:label="PorterAssistance">
    </activity>
    <activity
        android:name="com.helpfinder.app.PorterOtherServices"
        android:label="PorterOtherServices">
    </activity>

</application>

</manifest>

+
试试这个

将字符串数组中的第一个元素更改为选择

<string-array name="counties">
    <item name="id">Select</item>
    so on..
这样做

public class MainActivity extends ActionBarActivity {

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

        Spinner spinner = (Spinner) findViewById(R.id.spinner);

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

        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                switch (pos) {
                case 0:
                    Intent j = new Intent(this, JasperOptions.class);
                    startActivity(j);
                    break;
                case 1:
                    Intent k = new Intent(this, LakeOptions.class);
                    startActivity(k);
                    break;
                case 2:
                    Intent l = new Intent(this, LaporteOptions.class);
                    startActivity(l);
                    break;
                case 3:
                    Intent n = new Intent(this, NewtonOptions.class);
                    startActivity(n);
                    break;
                case 4:
                    Intent p = new Intent(this, PorterOptions.class);
                    startActivity(p);
                    break;
                }
            }

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

            }
        });

    }
}
公共类MainActivity扩展了ActionBarActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
微调器微调器=(微调器)findViewById(R.id.Spinner);
ArrayAdapter=ArrayAdapter.createFromResource(这个,R.array.countries,android.R.layout.simple\u微调器\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共位置(AdapterView父项、视图、整数位置、长id){
开关(pos){
案例0:
Intent j=新Intent(this,JasperOptions.class);
星触觉(j);
打破
案例1:
Intent k=新Intent(this,LakeOptions.class);
星触觉(k);
打破
案例2:
Intent l=新Intent(this,laportoptions.class);
星触觉(l);
打破
案例3:
意图n=新意图(此,NewtonOptions.class);
星触觉(n);
打破
案例4:
Intent p=新Intent(this,PorterOptions.class);
星形触觉(p);
打破
}
}
@凌驾
未选择公共无效(AdapterView arg0){
}
});
}
}

发布日志猫错误。同时检查您是否在清单中提供了所有活动。好的,学习如何在此处编辑我的评论,以便我可以。谢谢。是的,所有活动都在清单中。编辑您的问题帖子错误。只需点击问题标签下方的“编辑”,然后发布错误日志。没有看到您在此处登录,可能有一些原因。1。当您启动主活动时,它将自动启动“JasperOptions.class”(即交换机中的案例0),这会导致崩溃吗?2.你有这些活动的课堂文件吗?3.您在微调器中有多少个位置?@user3474924很高兴这有帮助。快乐编码。
spinner.setSelection(0);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int pos, long arg3) {
            // TODO Auto-generated method stub
            switch(pos) {
                  case 1:
                      Intent j = new Intent ( this, JasperOptions.class);
                      startActivity(j);
                      break;
                  case 2:
                      Intent k = new Intent(this, LakeOptions.class);
                      startActivity(k);
                      break;
                  case 3:
                      Intent l = new Intent(this, LaporteOptions.class);
                      startActivity(l);
                      break;
                  case 4:
                      Intent n = new Intent(this, NewtonOptions.class);
                      startActivity(n);
                      break;
                  case 5:
                      Intent p = new Intent(this, PorterOptions.class);
                      startActivity(p);
                      break;
              }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });
public class MainActivity extends ActionBarActivity {

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

        Spinner spinner = (Spinner) findViewById(R.id.spinner);

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

        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                switch (pos) {
                case 0:
                    Intent j = new Intent(this, JasperOptions.class);
                    startActivity(j);
                    break;
                case 1:
                    Intent k = new Intent(this, LakeOptions.class);
                    startActivity(k);
                    break;
                case 2:
                    Intent l = new Intent(this, LaporteOptions.class);
                    startActivity(l);
                    break;
                case 3:
                    Intent n = new Intent(this, NewtonOptions.class);
                    startActivity(n);
                    break;
                case 4:
                    Intent p = new Intent(this, PorterOptions.class);
                    startActivity(p);
                    break;
                }
            }

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

            }
        });

    }
}