-3

这是我的 Help.java。首先在我的项目中,我显示一个弹出窗口,如果数据库没有联系,则会发出警告。然后当用户单击确定时,它会显示包含如何创建联系人的帮助。在此活动中,我添加了选项菜单。但是当我单击选项 1 时,什么也没有发生。

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>
4

3 回答 3

2

尝试这个-

 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);
}
于 2013-06-19T04:53:23.780 回答
2

像这样更改您的代码

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

我测试了这段代码,这工作正常..

于 2013-06-19T04:28:36.747 回答
0

发生这种情况是因为您正在使用 getApplicationContext()。只需替换如下:

startActivity(new Intent(YourActivity.this, Add_item.class));
于 2013-06-19T04:37:31.410 回答