Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

android - Can't go to a new activity from selected option from option menu

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...