2011-12-02 80 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

嗨,感謝您的答覆。我將在本週晚些時候嘗試此代碼,並正確回覆結果。非常感謝。 –