2011-12-02 77 views
0

我设置了contextMenu,它显示列表中的项目。但是,当选择项目时,它什么也不做。我尝试了各种方法;使用.setCheckable()或调用意图。我不接受任何错误,但它没有做任何事情。同一个班级可以通过Intent从一个单独的按钮调用,并且工作正常。ContextMenu已选择项目但无法启动活动(意图)

我想我错过了代码中的一些重要细节。

`package com.myExperiment.androidapp.userinterface; 

import android.app.Activity; 
//import android.app.ListActivity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.ContextMenu; 
import android.view.ContextMenu.ContextMenuInfo; 
//import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
//import android.view.ViewGroup; 
import android.widget.Button; 
//import android.widget.ListView; 
//import android.widget.Toast; 


public class MyHome extends Activity 
{ 

    Button btnMyMenu; 
    Button btnAnnouncements; 
    Button btnGoToWebsite; 
    Button btnWorkflows; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.homelayout); 

     //Initialising variables with xml properties 
     btnMyMenu = (Button) findViewById(R.id.buttonMyStuff); 
     btnAnnouncements = (Button) findViewById(R.id.buttonAnnouncements); 
     btnGoToWebsite = (Button) findViewById(R.id.buttonWebsite); 
     btnWorkflows = (Button)findViewById(R.id.buttonWorkflow); 

     //Checking for button click listeners 
     btnMyMenu.setOnClickListener(actionOnClickListener); 
     btnGoToWebsite.setOnClickListener(buttonGoToWebsite); 
     btnWorkflows.setOnClickListener(buttonWorkflows); 

     //checking for context menu cluck 
     registerForContextMenu(btnMyMenu); 

    }//onCreate 


    View.OnClickListener buttonWorkflows = new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      Intent workflowIntent = new Intent(MyHome.this, Workflows.class); 
      startActivity(workflowIntent); 
     } 
    }; 


    //Open myExperiment website when button clicked 
    View.OnClickListener buttonGoToWebsite = new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      String website = "http://www.myexperiment.org"; 
      Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(website)); 
      startActivity(browse); 
     } 
    }; 



    private View.OnClickListener actionOnClickListener = new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      if (v != null) 
      { 
       v.showContextMenu(); 
      } 

     } 
    }; 


    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
    { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.setHeaderTitle("My Menu"); 
     menu.add(0, v.getId(), 0, "Workflows"); 
     menu.add(0, v.getId(), 0, "Credits"); 
     menu.add(0, v.getId(), 0, "Friends"); 
     menu.add(0, v.getId(), 0, "Groups"); 
     menu.add(0, v.getId(), 0, "Notifications"); 

    }//onCreateContextMenu 

    public boolean onCreateItemSelected(MenuItem item) 
    { 
     if(item.getTitle() == "Workflows") 
     { 
      Intent workflowIntent = (Intent) item.setIntent(new Intent(MyHome.this, Workflows.class)); 
      startActivity(workflowIntent); 


     } 
     else if(item.getTitle() == "Credits") 
     { 


        Intent creditsIntent = new Intent(MyHome.this, Credits.class); 
        startActivity(creditsIntent); 

     } 
     else if(item.getTitle() == "Friends") 
     { 

      View.OnClickListener buttonFriendsHandler = new View.OnClickListener() 
      { 

       @Override 
       public void onClick(View v) 
       { 
        Intent friendsIntent = new Intent(MyHome.this, Friends.class); 
        startActivity(friendsIntent); 

       } 
      }; 

     } 


     else 
     { 
      return false; 
     } 


     return true;  

    }//onCreateItemSelected 


} 

回答

0

您的标题匹配不起作用。将其更改为item.getTitle().equals("Workflows")

更好的是,使用id匹配而不是字符串匹配。添加您的菜单项是这样的:

int groupId = v.getId(); 
menu.add(groupId, 0, ContextMenu.NONE, "Workflows"); 
menu.add(groupId, 1, ContextMenu.NONE, "Credits"); // etc 

而且,你不需要设置的菜单项的意图。所以,在onCreateItemSelected

public boolean onCreateItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case 0: 
     Intent workflowIntent = new Intent(this, Workflows.class); 
     startActivity(workflowIntent); 
     break; 
    case 1: 
     Intent creditsIntent = new Intent(MyHome.this, Credits.class); 
     startActivity(creditsIntent); 
     break; 
    // etc 
    } 
} 
+0

嗨,感谢您的答复。我将在本周晚些时候尝试此代码,并正确回复结果。非常感谢。 –