0

i am very new in android development (eclipse).i am try to do the following things. 1. generate a menu list when click on menu button in the eclipse 2. when clicking on the menu item navigate another page(xml page). i use the following codes.

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/about" android:title="About"/>
<item android:id="@+id/help" android:title="Help" />
</menu>

MainActivity.java

package com.example.menuoptions;

import com.example.menuoptions.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.about:
    startActivity(new Intent(this, About.class));
    return true;
    case R.id.help:
    startActivity(new Intent(this, Help.class));
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
}
}

About.java

 package com.example.menuoptions;
 import android.app.Activity;
 import android.os.Bundle;
 public class About extends Activity {
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);

   }
  }

about.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="About Page" />

</LinearLayout>

Problem is in my second need. The menu items are listed but when clicking on the about item it is not navigate to about.xml.

3
  • Getting any error ?? if so can you post your logcat? Commented Sep 20, 2013 at 6:29
  • What is the error that you are getting? Can you post the logcat? Commented Sep 20, 2013 at 6:29
  • @Beginner: no nothing happend. stay in main.xml Commented Sep 20, 2013 at 6:30

3 Answers 3

1

you should use this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
    return super.onOptionsItemSelected(item);
}
}
Sign up to request clarification or add additional context in comments.

3 Comments

i din't notice this public boolean onOptionsItemSelected(MenuItem item). sorry my mistake
@Parvathiiiii what did it throws and at where...??
@PiyushGupta i din't notice the change. so i deleted my previous comment. its not your fault
0

have you provided the permission in manifest file?

 <activity android:name=".About" />

2 Comments

this is not a permission its an entry for the activity in manifest
@Anil: there is no change in the answer
0
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

simple code for menu selected

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.about:
        startActivity(new Intent(this, About.class));
        return true;
    case R.id.help:
        startActivity(new Intent(this, Help.class));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

Try to use this

1 Comment

What is the R.id.new_game: case??/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.