Note that there are some explanatory texts on larger screens.

plurals
  1. POLaunching activity from android options menu
    text
    copied!<p>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.</p> <p>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 </p> <pre><code>Intent cannot be resolved to a type </code></pre> <p>in home.java on the line:</p> <pre><code>Intent intent = new Intent(this, about.class); </code></pre> <p>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.</p> <p>I have my menu defined in res/menu/main_menu.xml by:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+id/home" android:icon="@drawable/ic_menu_home" android:title="@string/home" /&gt; &lt;item android:id="@+id/about" android:icon="@drawable/ic_menu_about" android:title="@string/about" /&gt; &lt;/menu&gt; </code></pre> <p>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.</p> <pre><code>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); } } } </code></pre> <p>About.java is the new activity to be launched and is shown below:</p> <pre><code>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; } } </code></pre> <p>I have changed the manifest to:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ca.example.home" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="8" /&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"&gt; &lt;activity android:name=".home" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".about"&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload