Android start activity on Menu Item selection

gdawgrancid picture gdawgrancid · Dec 16, 2010 · Viewed 31.3k times · Source

I have 2 classes. One will be a basic instructions screen and on that screen it will have a menu that will let you go to the other class. The other class is a MapActivity. I believe the problem is that its not finding the other class. I've tried a few different ways of declaring the intent to find the class. This is the latest thing I've tried:

@Override
public boolean onCreateOptionsMenu(Menu menu){        
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.goToMap:
        Intent intent = new Intent();
        intent.setClassName(Main.this, "Map.Class");
        startActivity(intent);
        return true;            
    }
    return false;
}

Its a basic class that extends Activity and the map class is a basic class that extends MapActivity (can that cause a problem?). And here is the important part of my Manifest file:

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Campus_Map"
              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=".Main" android:label="Instructions" ></activity>
    <activity android:name=".Map" android:label="Map">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

</application>

Edit: When looking at the LogCat to figure out what was happening, I'm getting a java.lang.NoClassDefFoundError and a few other messages saying "Link of class ./Map failed", "Could Not find class ./Map referenced from method ./Main.run" and "VFY: unable to resolve const-class 37"

Answer

Balaji Khadake picture Balaji Khadake · Jun 4, 2011

You can use like this and don't forget to add both activities to AndroidManifest.xml:

Intent launchNewIntent = new Intent(CurrentClass.this,SecondClass.class);
startActivityForResult(launchNewIntent, 0);