Question

I am completely new to Java so I want to apologize and ask for your understanding for this question. I am trying to use a spinner that can switch to other activities. Any time I run the emulator, I get an Unfortunately, your application has stopped error. Do I need to add something other than the code below to my activity I am switching to?

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

}

Here is my code from the activity attached to the xml with my spinner on it. Do I need to add anything to the xml spinner tag? Again, I apologize for my ignorance.

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

    }

my logcat errors are as follows

    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.

This is my manifest

<?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>
Was it helpful?

Solution

Try this..

Change your first element from string array as Select

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

And after user selecting spinner you can start activity below codes inside onCreate

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

        }

    });

OTHER TIPS

Do this way

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) {

            }
        });

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top