Reputation: 293
I am currently trying to implement a Spinner within my android application. I am having trouble with getting the OnItemSelected method, to open a new class based on what item was selected.
I have the code shown bellow, which does not seem to work, Also since adding this is, it now from the menu when I click the button to open Film and TV it opens the wrong layout, but nothing changed other than adding the bellow code.
What should happen: Activity Starts --> Click on Film and TV --> Select item from Spinner --> New class opens based on what Item was selected.
What Happens now: Activity Starts --> Click on Film and TV --> Wrong layout opens --> Press back on phone --> Right layout opens --> Select item from Spinner --> Nothing Happens
Code:
String classes[] = {"SeanConnery", "BillyConnoly", "JamesMcAvoy", "KarenGillan", "KellyMacdonald", "AshleyJensen"};
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String classSpot = classes[pos];
try{
Class nextClass = Class.forName("com.example.famouspeople." + classSpot);
Intent ourIntent = new Intent(Film.this, nextClass);
startActivity(ourIntent);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.famouspeople"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.example.famouspeople.MainMenu"
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.example.famouspeople.Film"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.SeanConnery"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.BillyConnoly"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.JamesMcAvoy"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.KarenGillan"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.AshleyJensen"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.famouspeople.KellyMacdonald"
android:label="@string/app_name" >
</activity>
</application>
Upvotes: 1
Views: 267
Reputation: 28799
change your mainfest
from
<activity
android:name="com.example.famouspeople.SeanConnery"
android:label="@string/app_name" >
</activity>
To
<activity
android:name=".YourJavaClassName"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.famouspeople.SeanConnery" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Upvotes: 1
Reputation: 8853
to create intent you can do something like this...
final Context context = this;
Intent intent = new Intent(context,youractivity.class);
startActivity(intent);
Upvotes: 2