android - 使用微调器切换到另一个 Activity

标签 android android-intent switch-statement android-spinner

我是 Java 的新手,所以我想向您道歉并请求您理解这个问题。 我正在尝试使用可以切换到其他 Activity 的微调器。每当我运行模拟器时,我都会收到一个 Unfortunately, your application has stopped 错误。我是否需要在要切换到的 Activity 中添加除以下代码之外的其他内容?

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

}

这是我的代码,来自附加到 xml 的 Activity ,上面有我的微调器。我需要向 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);
}

    }

我的logcat错误如下

    03-28 18:24:28.320    1928-1928/com.helpfinder.app D/dalvikvm﹕ GC_FOR_ALLOC freed 65K,                    5% free 3043K/3176K, paused 185ms, total 199ms
    03-28 18:24:28.480    1928-1928/com.helpfinder.app I/dalvikvm-heap﹕ Grow heap (frag case) to 6.472MB for 3609872-byte allocation
    03-28 18:24:28.940    1928-1937/com.helpfinder.app D/dalvikvm﹕ GC_FOR_ALLOC freed 2K, 3% free 6565K/6704K, paused 451ms, total 451ms
   03-28 18:24:32.030    1928-1928/com.helpfinder.app D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
   03-28 18:24:47.750    1928-1928/com.helpfinder.app D/dalvikvm﹕ GC_FOR_ALLOC freed 66K, 2% free 6769K/6900K, paused 94ms, total 99ms
    03-28 18:24:56.250    1928-1928/com.helpfinder.app I/Choreographer﹕ Skipped 139 frames!  The application may be doing too much work on its main thread.
    03-28 18:24:56.960    1928-1928/com.helpfinder.app W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
    03-28 18:24:57.070    1928-1928/com.helpfinder.app W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
    03-28 18:24:57.070    1928-1928/com.helpfinder.app W/ViewRootImpl﹕ Dropping event due to root view being removed: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=181.0, y[0]=531.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2456198, downTime=2456110, deviceId=0, source=0x1002 }
    03-28 18:24:57.200    1928-1928/com.helpfinder.app W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed.
    03-28 18:24:57.200    1928-1928/com.helpfinder.app W/ViewRootImpl﹕ Dropping event due to root view being removed: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=181.0, y[0]=531.0,    toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2456404, downTime=2456404, deviceId=0,  source=0x1002 }
    03-28 18:25:06.000    1928-1928/com.helpfinder.app I/Choreographer﹕ Skipped 57 frames!  The application may be doing too much work on its main thread.
    03-28 18:25:26.750    1928-1928/com.helpfinder.app I/Choreographer﹕ Skipped 55 frames!  The application may be doing too much work on its main thread.

这是我的 list

<?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..

用户选择微调器后,您可以在 onCreate 内的代码下方开始 Activity

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

        }

    });

关于android - 使用微调器切换到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22726897/

相关文章:

android - 为什么AppWidgetManager要更新所有RemoteViews for Widgets的Intent?

go - 开关盒不同类型与去

java - 类 'Anonymous class derived from PlaceSelectedListener' 必须声明为抽象或实现抽象方法

android - 运行非离线语言时的 SpeechRecognizer ERROR_SERVER

android - 应用程序在 Camera.Release() 上卡住

android - 使用 altbeacon 的应用程序不显示距离

android - 通过 Intent 发送短信

android - 空指针异常 : Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

Java:If 与 Switch

javascript - 尝试从 switch 语句中的函数返回值 -JS-