Launching activity from android options menu

bpmccain picture bpmccain · Jul 11, 2011 · Viewed 16.9k times · Source

I have looked through these forums to find a solution to this problem, and even though there appear to be solutions, none of them seem to be working for me. So here goes.

I am a newbie to Android development. I have an app with an options menu. When I click on one to the options, I want it to launch a new activity - but I keep getting the error

Intent cannot be resolved to a type

in home.java on the line:

Intent intent = new Intent(this, about.class);

Below is all of my code that I believe is relevant. Please let me know if you need to see anything else. As I said, I have tried to follow other questions, but none of them seem to work for me (as-in the below code seems to work for everyone else). Any help would be awesome.

I have my menu defined in res/menu/main_menu.xml by:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/home"
          android:icon="@drawable/ic_menu_home"
          android:title="@string/home" />
    <item android:id="@+id/about"
          android:icon="@drawable/ic_menu_about"
          android:title="@string/about" />
</menu>

I have two activities - home.java and about.java. Home.java is the activity that is launched when the app is launched and is shown below.

package ca.example.home;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class home extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.home:
            return true;
        case R.id.about:
            Intent intent = new Intent(this, about.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

About.java is the new activity to be launched and is shown below:

package ca.brianmccain.nbla;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;

public class about extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
}

I have changed the manifest to:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ca.example.home"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".home"
                  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=".about">
        </activity>
    </application>
</manifest>

Answer

user658042 picture user658042 · Jul 11, 2011

You have to import the intent class.

import android.content.Intent;

If you ever get a similar error, and if you are using eclipse, press Ctrl-Shift-O ("Organize imports") - this searches for all required imports and adds them to the file.