-3

this my Help.java. At first in my project I show a popup window that warns if the database has no contact. Then when user clicks ok It shows Help that includes how to create contact. In this activity I added my option menu.But when I click option1 nothing happens.

package com.pandroid.phonebook;
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 Help extends Activity {             

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.helpmenu);                  
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater =getMenuInflater();
        inflater.inflate(R.menu.options, menu);
        return true;
    } 
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()) {
            case R.id.item1:
                 startActivity(new Intent(Help.this, Add_item.class));                                   
                 break;                             
            case R.id.item2:
                 startActivity(new Intent(Help.this, Add_item2.class));                                  
                 break;                    
            case R.id.item3:
                 startActivity(new Intent(Help.this, Add_item3.class));
                 break;
            case R.id.item4:
                 startActivity(new Intent(Help.this, Add_item4.class));
                 break;
            default:
                 return super.onOptionsItemSelected(item);
        }
        return true;
     }

 }

R.menu.options:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:id="@+id/item1"  android:title="@string/op1" android:icon="@android:drawable/ic_menu_add"/>
   <item android:id="@+id/item2"  android:title="@string/op2" android:icon="@android:drawable/ic_menu_search"/>
   <item android:id="@+id/item3"  android:title="@string/op3" android:icon="@android:drawable/ic_menu_manage"/>
   <item android:id="@+id/item4"  android:title="@string/op4" android:icon="@android:drawable/ic_menu_directions"/>

</menu>
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166

3 Answers3

2

Change your code like this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options, menu);
    return super.onCreateOptionsMenu(menu);
} 

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()) {
        case R.id.item1:
             startActivity(new Intent(Help.this, Add_item.class));                                   
             break;                             
        case R.id.item2:
             startActivity(new Intent(Help.this, Add_item2.class));                                  
             break;                    
        case R.id.item3:
             startActivity(new Intent(Help.this, Add_item3.class));
             break;
        case R.id.item4:
             startActivity(new Intent(Help.this, Add_item4.class));
             break;
        default:
             return super.onOptionsItemSelected(item);
    }
    return true;
 }

I tested this code this is working fine..

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • also remove the [`finish()`](http://stackoverflow.com/questions/14411477/how-to-stop-an-activity-in-android-using-intent). – Gunaseelan Jun 19 '13 at 04:35
  • I have been trying to add one item first,,Is it necessary to have different selection handlers? I am not getting any error. just when I click option menu item1, it dont go to another activity. And in logcat there is a tag called dalvikvm.. thank u. – Shamminuj Rahman Jun 19 '13 at 04:43
  • Show me your code what changes you have done..and log cat errors also if it has. – RajaReddy PolamReddy Jun 19 '13 at 04:44
  • 1
    i Was updated my answer that is working fine look at that. let me know the result. Sure you have to declare your Add_item classes in manifest file.. show me that manifest file code also it will be easy to find errors. other than that there is no errors in that above code. if you get errors post logcat messages.. – RajaReddy PolamReddy Jun 19 '13 at 05:46
2

try this-

 public boolean onMenuItemSelected(int featureId, MenuItem item) {

    switch(item.getItemId()) {          

    case R.id.item1:
                         startActivity(new Intent(Help.this, Add_item.class));

                         return true;

                     case R.id.item2:
                         startActivity(new Intent(Help.this, Add_item2.class));

                         return true;                    
                     case R.id.item3:
                         startActivity(new Intent(Help.this, Add_item3.class));
                         return true;
                     case R.id.item4:
                         startActivity(new Intent(Help.this, Add_item4.class));
                         retuen true;
}
    return super.onMenuItemSelected(featureId, item);
}
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
0

It happens because you are using getApplicationContext(). just replace like below:

startActivity(new Intent(YourActivity.this, Add_item.class));
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99