Android start activity on Menu Item selection

2019-02-06 23:42发布

问题:

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"

回答1:

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


回答2:

Have you tried this way?

Intent intent = new Intent(Main.this, Map.class);


回答3:

case R.id.home:
    startActivity(new Intent(main.this, map.class));
    return true;


回答4:

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