Android start activity on Menu Item selection
Asked Answered
M

4

12

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"

Mcatee answered 16/12, 2010 at 2:0 Comment(0)
W
20

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);
Wean answered 4/6, 2011 at 8:34 Comment(0)
W
3

Have you tried this way?

Intent intent = new Intent(Main.this, Map.class);
Wait answered 16/12, 2010 at 3:0 Comment(0)
P
2
case R.id.home:
    startActivity(new Intent(main.this, map.class));
    return true;
Pandean answered 29/8, 2013 at 16:26 Comment(0)
S
1

You have to implement this line into your Manifest

<uses-library android:name="com.google.android.maps" />

If you did this already you did it wrong! It has to be within the <application> </application> tag. Otherwise you will get your error

Surtax answered 14/2, 2012 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.